Truthy and Falsy
These are used on how non-boolean data types, like strings or numbers, are evaluated when checked inside a condition.
Sometimes you'll want to check if a variable exists and you won't necessarily want it to equal to a specific value.
Example:
In the above example the if
statement block runs because it has a truthy value; even though the value of myVaribale
is not explicitly the value true
.
When used in a boolean or conditional context, it evaluates to true
because it has been assigned a non-falsy value.
The list of falsy values includes:
0
Empty strings like "" or ''
undefined
which represent when a declared variable lack a valueNaN
, or Not a Number
Example:
In the above example the if
condition evaluates to false
because numberOfApples
has a falsy value which is 0
.
Example:
Last updated