All Truthy and Falsy Javascript Values

All Truthy and Falsy Javascript Values

In Nodejs, every value has an associated boolean, true or false, value. For example, a null value has an associated boolean value of false. A string value, such as abc has an associated boolean value of true. Values that are associated with boolean true are said to be truthy. Values that are associated with boolean false values are said to be falsy.

It can get confusing to keep track of every Javascript value type and its associated "truthy-ness", so I added the two sets of examples below so I could keep track.

Truthy

if (true) // boolean -> true
if ({}) // any object (empty or not) -> true
if ([]) // any array (empty or note) -> true
if (42) // any non-zero interger -> true
if ("0") // any string -> true
if ("false") // any string including "false" -> true
if (new Date()) // any Date object -> true
if (-42) // any negative number -> true
if (12n) // BigInt, 12n is truthy. -> true
if (3.14) // any non-zero float -> true
if (-3.14) // any negative float -> true
if (Infinity) // -> true
if (-Infinity) // -> true

Falsy

if (false) // The keyword false -> false
if (0) // The number zero -> false
if (-0) // The number negative zero -> false
if (0n) // BigInt, 0n is falsy. -> false
if ("") // Empty string value -> false
if (null) // -> false
if (undefined) // -> false
if (NaN) // not a number -> false

Hope this was helpful, catch me on twitter for more blog posts at @mattccrampton

Pause reading for 5 seconds...
Join My Mailing List!
Note: I will never share your email with anyone else.
Awesome! Thanks so much.
Ok, lets continue.
Post A Twitter Response To This Blog Post

To: @mattccrampton

0