JavaScript Code


JavaScript If with OR Operator


If Statement with OR

if (condition1 || condition2) {
  //code
}
  • if is keyword
  • condition1 and condition2 are two individual boolean expressions which are joined by OR operator
  • || is JavaScript OR operator

if at least one of the condition1 or condition2 is true, then if-block code is run

Examples

1. check if number in a is divisible by at least one of the numbers: 3, 7

a = 42;

if ((a % 3 == 0) || (a % 7 == 0)) {
  console.log("a is divisible by 3 or 7, or both");
}

2. check if string in x contains the vowel character a or e or both

x = 'apple';

if (x.includes('a') && x.includes('e')) {
  console.log("given string contains the vowel 'a' or 'e' or both");
}

References