Truthy and falsy values
In JavaScript, some values are considered truthy
and others are considered falsy
. A value is considered truthy
if it's not one of the following:
- false
- 0
- '' (an empty string)
- null
- undefined
- NaN
All other values are considered truthy
. This can be useful in conditional statements, as you don't need to explicitly compare a variable to a specific value. For example:
Loading Code . . .
In this example, the code will output 'Hello, Alice'
because the name
variable is truthy
(it's not ''
, null
, undefined
, etc.). If the name variable were falsy
(e.g., name = ''
), the code would output 'Hello, stranger'
.
In the following exercises, you'll be asked to select all the truthy
values.