What is the use of && operator in JavaScript ?

The && operator, known as the logical AND operator, is used to evaluate two operands and returns true if both operands are true, otherwise, it returns false.

The Logical AND(&&) Operator can also be used on non-boolean values also. AND operator has higher precedence than the OR operator.

Syntax:

a&&b

Example: Here, x > 5 evaluates to true because 7 is greater than 5, and y < 10 evaluates to false because 12 is not less than 10. However, since the && operator requires both conditions to be true, the console.log() statement inside the if block will not be executed. Instead, the code inside the else block will be executed, printing “At least one condition is false.”

Javascript




let x = 7;
let y = 12;
 
if (x > 5 && y < 10) {
    console.log("Both conditions are true."); // This line will not be executed
} else {
    console.log("At least one condition is false.");
}


Output

At least one condition is false.

Contact Us