Member-only story
The Six Falsy Values in JavaScript
undefined
null
NaN
0
""
false
Everything else is truthy. (i.e. [], {}, "false"
)
&& and || Return values
A good rule of thumb for remembering the return values of these operators.
JavaScript will want to do the least amount of work to get its return value.
&&: will first try to return the first falsy value. If none found, it will return the last truthy value.
||: will first try to return the first truthy value. If none found, it will return the last falsy value.
const falsy1 = null;const falsy2 = NaN;const truthy1 = 'c';const truthy2 = 'd';// both are truthyconsole.log(truthy1 && truthy2); // truthy2console.log(truthy1 || truthy2); // truthy1// both are falsyconsole.log(falsy1 && falsy2); // falsy1console.log(falsy1 || falsy2); // falsy2// 1st truthy, 2nd falsyconsole.log(truthy1 && falsy1); // falsy1console.log(truthy1 || falsy1); // truthy1// 1st falsy, 2nd truthyconsole.log(falsy1 && truthy1); // falsy1console.log(falsy1 || truthy1); // truthy1