Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The logical AND (&&) operator evaluates operands from left to right, returning the first falsy operand it encounters. If all operands evaluate to a truthy value, it returns the value of the last operand. Crucially, it performs short-circuit evaluation and returns the actual underlying value of the operand, not necessarily a boolean. Syntax
expr1 && expr2 && expr3
Evaluation Mechanics
  1. Left-to-Right Evaluation: The operator assesses expr1.
  2. Type Coercion: It implicitly coerces expr1 to a boolean to determine if it is truthy or falsy.
  3. Short-Circuiting: If expr1 is falsy, the operator immediately returns the exact value of expr1 and terminates evaluation. Subsequent expressions are completely ignored.
  4. Continuation: If expr1 is truthy, the operator proceeds to evaluate the next expression, repeating the process until it either finds a falsy value or reaches the final operand.
Falsy Values in JavaScript The && operator will short-circuit upon encountering any of the following strictly falsy values: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN. Behavioral Examples Returning the first falsy value:
const result1 = "text" && 0 && true; // Returns 0
const result2 = null && "string";    // Returns null
const result3 = NaN && undefined;    // Returns NaN (stops at the first falsy value)
Returning the last truthy value:
const result4 = true && "Success";   // Returns "Success"
const result5 = 1 && 2 && 3;         // Returns 3
const result6 = [] && {};            // Returns {} (empty arrays and objects are truthy)
Operator Precedence The && operator has a higher precedence than the logical OR (||) operator, but a lower precedence than equality and relational operators (like ===, >, <).
// The && evaluates first: true || (false && false)
const precedenceExample = true || false && false; // Returns true

// Comparison evaluates before &&: (5 > 3) && (10 < 20)
const comparisonExample = 5 > 3 && 10 < 20;       // Returns true
Master JavaScript with Deep Grasping Methodology!Learn More