Or operator
More complex decisions sometimes allow one or another thing to be true. The || operator allows you to check if either operand expression is true (or both), and then possibly perform some specific action using an if, if-else, or while block.
Examples
Example: Typical Usage
Truth table for the boolean OR operator.
console.log(true || true);
console.log(true || false);
console.log(false || true);
console.log(false || false);

Example: Take Your Temperature
Check for temperature outside a good range or not.
// Check for temperature outside 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.");
}
});
Example: Thank Goodness It's Friday!
Determines if it is currently the weekend.
// Determines if it is currently the weekend.
var now = new Date();
var dayOfWeek = now.getDay();
var isWeekend = false;
if (dayOfWeek == 0 || dayOfWeek == 6) {
isWeekend = true;
}
console.log(isWeekend);
Syntax
expression1 || expression2
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.