{{ message }}

Instantly share code, notes, and snippets.

# romankierzkowski/0_falsy_.js

Last active Feb 22, 2020
True, False and Equal in JS
 /* false, 0, undefined, null, NaN, "" are false */ > Boolean(false) false > Boolean(0) false > Boolean("") false > Boolean(undefined) false > Boolean(null) false > Boolean(NaN) false
 /* Anything else is true: */ > Boolean(true) true > Boolean(1) true > Boolean(2) true > Boolean("Hello world!") true /* Including empty array, Python developers!!! */ > Boolean([]) true
 /* Let's try equal: */ > false == false true > 0 == false true > "" == false true /* So far, so good, but... : */ > undefined == false false > null == false false > NaN == false false
 /* The same for truthy works well: */ > true == true true > 1 == true true /* But: */ > 2 == true false > "Hello world!" == true false > [] == true false
 /* Let's avoid type coerition with triple-equals: */ > false === false true > 0 === false false > "" === false false > undefined === false false > null === false false > NaN === false false /* Good. And for truthy: */ > true === true true > 1 === true false > 2 === true false > [] === true false > "Hello world!" === true false /* Tripple equals returns always false when types of operands does not match." */
 /* Equal is 'generous' about the type: */ > true == '1' true > true == '1.0' true /* Triple equals is not: */ > true === '1' false > true === '1.0' false /* It only goes with numbers: */ > 2 == '2' true /* But: */ > true == 'true' false
 > Boolean(' ') true > Boolean(' \n\n') true /* But: */ > ' ' == 0 true > ' \n' == 0 true > ' \n' == false true /* o_O ?? WAT */ /* Thank god for === */ > ' ' === 0 false > ' \n' === 0 false > ' \n' === false false
 /* These are quite obvious: */ > undefined == undefined true > undefined === undefined true > null == null true > null === null true /* These are not: */ > null == undefined true > null === undefined false /* But to check if global variable is not defined you can't do the following: */ > foo === undefined ReferenceError: foo is not defined /* There is an exception because foo is not defined. That's why: */ > typeof foo === 'undefined' true /* Typeof returns string containing name of a type. And undefined has type undefined, whereas: */ > typeof null "object" > typeof undefined "undefined"
 /* And these are useful for making default result: */ > null || "abc" "abc" > undefined || "abc" "abc" > NaN || "abc" "abc"
 /* Watch out for NaN! */ > NaN + NaN NaN // NaN are not equal: > NaN == NaN false > NaN === NaN false // Think about it! A dog and a cat are Not a Number, but they are not equall as well. It make sense. // Use Number.isNaN() instead: > Number.isNaN(NaN) true > Number.isNaN(0) false > NaN && NaN NaN > NaN || NaN NaN // But: > NaN || true true > true || NaN true > false && NaN false > NaN && false NaN // It all make sense, because condition is not evaluated after it's result is certain. // But now: > NaN | NaN 0 > NaN & NaN 0 // o_O ?? //

### kybernetikos commented Feb 17, 2014

 ``````[[["\t\n 987654321e-400"]]] == 0 `````` this is because during type coercion of an object to a number, the object is toStringed. Arrays don't have any wrapping in their string representation, so any level of nesting of arrays containing a single item will turn into the same string. During string coercion to number, leading whitespace is ignored.

### luisgerhorst commented Feb 17, 2014

 Nice summary! At some points JavaScript is just stupid but `===` makes things a lot better.