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
To: @mattccrampton
0
Other Posts
When exporting photos from a service like Flickr, perhaps after they've given notice that they're going to delete our photos if you don't subscribe to......
Google Forcing Nest Cameras Visual Indicator Light To Be On Received the following email from Google today... Full email text... Recently, we shared our commitment......
Posting to Twitter with Python - Part Two: Posting Photos NOTE: This is part two of my posting to Twitter with Python tutorial. If you......
Doubleclick to open a file in VIM from OSX I use VIM for just about everything from note taking to coding to keeping track of......
Sign Into Gmail Without Signing Into Google Chrome Unfortunately, Google has made changes to Chrome since this blog post was posted which removed the options......
Four Steps To Write An AppleScript to Gather ALL Windows on OSX I have no idea why, but there is no built-in way to gather......