Category: Math
Returns true when either expression is true and false otherwise.
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.
// 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: TGIF 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);
expression1 || expression2
Name | Type | Required? | Description |
---|---|---|---|
expression1 | boolean | Yes | The first boolean expression to evaluate. |
expression2 | boolean | Yes | The second boolean expression to evaluate |
Boolean true or false
Found a bug in the documentation? Let us know at documentation@code.org
__ || __
Boolean (true or false)
Found a bug in the documentation? Let us know at documentation@code.org