VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-logical-operators/

⇱ JavaScript Logical Operators - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript Logical Operators

Last Updated : 16 Jan, 2026

Logical operators in JavaScript are used to combine or modify boolean values to make decisions. They help control program flow by evaluating whether one or more conditions are true or false.

  • They are widely used in if statements, loops, and conditions.
  • Logical operators make it easier to handle multiple conditions at once.
👁 3


1. Logical AND (&&) Operator

The logical AND (&&) operator checks whether both operands are true. If both are true, the result is true. If any one or both operands are false, the result is false.

It works with numbers as well, treating 0 as false and any non-zero value as true. It treats false, 0, -0, "", null, undefined, NaN and document.all as false.

In JavaScript, the && operator doesn't return true or false unless explicitly working with boolean values. Instead, it returns the actual value of the last operand evaluated:

  1. If the first operand (x) is falsy (like 0, null, undefined, false), it stops and returns that value.
  2. If the first operand is truthy, it evaluates the second operand and returns its value.

2. Logical OR (||) Operator

The logical OR (||) operator checks whether at least one of the operands is true. If either operand is true, the result is true. If both operands are false, the result is false.

Rules for ||:

  1. If the first operand is truthy, it stops and returns that value.
  2. If the first operand is falsy, it evaluates the second operand and returns its value.

Truthy and Falsy Values in JavaScript

  1. Falsy values: false, 0, null, undefined, NaN, and "" (empty string).
  2. Truthy values: Anything not falsy.

3. Logical NOT (!) Operator

The logical NOT (!) operator inverts the boolean value of its operand. If the operand is true, it returns false. If the operand is false, it returns true.

Logical NOT Works for Non-Boolean Values

Unlike && and ||, the logical not operator always results in true or false. It consider falsy values (mentioned above with logical or) as false. And all other values as true.

4. Nullish Coalescing (??) Operator

The nullish coalescing operator (??) returns the right-hand operand when the left-hand operand is either null or undefined. Otherwise, it returns the left-hand operand.

Comment