Logical operator || in javascript, 0 stands for Boolean false?

  • ||expr1 || expr2 (Logical OR)

    Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false..

  • &&expr1 && expr2 (Logical AND)

    Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

All values in Javascript are either “truthy” or “falsy”.
The following values are equivalent to false in conditional statements:

  • false
  • null
  • undefined
  • The empty string "" (\ '')
  • The number 0
  • The number NaN

All other values are equivalent to true.


So… var test = 0 || -1 ; returns -1.

If it was var test = 0 || false || undefined || "" || 2 || -1 it would return 2


Logical operator on MDN

Leave a Comment