Member-only story

JavaScript Truthy/Falsy and Logical Operator Cheatsheet

Kevin Kim
1 min readSep 11, 2019

--

The Six Falsy Values in JavaScript

  1. undefined
  2. null
  3. NaN
  4. 0
  5. ""
  6. 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

--

--

No responses yet