Truthy and Falsy Things

In order to use flow control successfully, it’s important to understand which kinds of values are “truthy” and which kinds of values are “falsy.” Sometimes, values that seem like they should evaluate one way actually evaluate another.

Example 2.14: Values that evaluate to true

‘0’;
‘any string’;
[]; // an empty array
{}; // an empty object
1; // any non-zero number

Example 2.15: Values that evaluate to false

0;
”; // an empty string
NaN; // JavaScript’s “not-a-number” variable
null;
undefined; // be careful — undefined can be redefined!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s