HTML and CSS Reference
In-Depth Information
> typeof g
"undefined"
Objects
Other than the types outlined above, all data types in JavaScript are objects: this includes
arrays, functions, dates and user defined objects. Objects will be discussed at length in the
sections below.
Truthy and Falsey Values
Now that you have an understanding of the JavaScript data types, the next thing to under-
stand is that some values for these types evaluate to
true
, and some evaluate to
false
. For
instance, the following are all considered false in JavaScript:
• false
• 0 (zero)
• "" (empty string)
• null
• undefined
• NaN
In order to see this in action, simply ask if they are equal to
false
in the console:
> 0 == false
true
All other values represent
true
values.
As a consequence of this, it is possible to utilize a shortcut when evaluating the value of
variables in conditional statements. Instead of writing the following:
> if (a == undefined || a == null) {
a = 1;
}
It is possible to simply write: