And Operator
More complex decisions sometimes require two things to be true. The && operator allows you to check if both operand expressions are true, and then possibly perform some specific action using an if, if-else, or while block.
// Truth table for the boolean AND operator. console.log(true && true); console.log(true && false); console.log(false && true); console.log(false && false); |
![]() |
Examples
Working 9 to 5
Determines if it is currently working hours.
// Determines if it is currently working hours.
var now = new Date();
var hours = now.getHours();
var workHours = false;
if (hours >= 9 && hours < 17) {
workHours = true;
}
console.log(workHours);
Take Your Temperature
Check whether a given temperature is in a healthy range or not.
// Check for temperature in a good range or not.
textLabel("tempLabelID", "What is your temperature?");
textInput("tempID", "");
button("buttonID", "Submit");
textLabel("tempMessageID", "");
onEvent("buttonID", "click", function(event) {
setText("tempMessageID","");
var temp = getText("tempID");
if (temp >= 98 && temp <= 99.5) {
setText("tempMessageID", "Your temperature is fine.");
}
else {
setText("tempMessageID", "You may be sick.");
}
});
Syntax
__ && __
Parameters
Name | Type | Required? | Description |
---|---|---|---|
expression1 | boolean | The first boolean expression to evaluate. | |
expression2 | boolean | The second boolean expression to evaluate. |
Returns
Tips
- Some complex decisions using an && operator can sometimes be rewritten to use an || operator. It is fine to choose whichever reads clearest.
Found a bug in the documentation? Let us know at support@code.org.