Search code examples
javascriptconsole.log

Why does 1 && 2 return 2?


Why does the expression 1 && 2 evaluate as 2?

console.log("1 && 2 = " + (1 && 2));


Solution

  • && (and operator) returns the last (right-side) value as long as the chain is "truthy".

    if you would try 0 && 2 -> the result would be 0 (which is "falsy")

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators