Or operator

Category:Math

Or operator

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.

Examples


// 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);

Syntax

expression1 || expression2

Parameters

Name Type Required? Description
expression1 boolean Yes The first boolean expression to evaluate.
expression2 boolean Yes The second boolean expression to evaluate

Returns

Boolean true or false

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 documentation@code.org

Syntax

__ || __

Returns

Boolean (true or false)

Found a bug in the documentation? Let us know at documentation@code.org