JavaScript Code


JavaScript If with AND Operator


If Statement with AND

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

if both condition1 and condition2 are true, then if-block code is run

Examples

1. check if number in a is divisible by both 3 and 7

a = 42;

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

2. check if string in x contains both the vowel characters a and e

x = 'apple';

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

References