In simple terms, it's any value that evaluates to false, nullable, or empty.
False => Falsy
Null => Falsy
Undefined => Falsy
Empty String => Falsy
Array with no items inside => Falsy
Object without keys (Object.keys(obj).length === 0) => Falsy
Zero => Falsy
What this means is that if you have a variable:
And a condition where you want to check the variable:
You do not need to compare it to an empty string or null or undefined:
Or convert it to a boolean to validate if it's Falsy (as you'd in other strongly typed languages).
Instead, consider it as a Falsy value (like a boolean) and just put
! prefix and JavaScript will evaluate it for you:
What is Truthy? Well, the opposite of Falsy:
True
Number greater or less than zero
String with at least one character
Array or an Object with at least one item
Variable with a value, e.g. ('A', 1, {}, [])
Variety of Outcomes
Likewise, if the outcome is the opposite, it will evaluate to a Truthy value.
A common way to toggle between truthy and falsy values is to use the logical operators:
And (&&)
Or (||)
&&
When setting up a condition between values, the && operator will capture a value
that evaluates to False.
If either value in a condition is Falsy, the output will be false:
Only if both values are Truthy, the && operator will evaluate to true:
Keep in mind that && will evaluate all values in a condition until it finds
the first Falsy value. Otherwise, it will take the last value.
Validate user input
If Either condition fails, the statement will be evaluated as False.
Display dynamic content
In Web frameworks like React, you can display HTML elements (or components)
only when a certain condition is satisfied.
Avoid nullable values
A common mistake that developers make is to attempt to read a value
from a property whose parent is null/undefined.
To solve this, you first make sure that the root property is not null/undefined
and then try to read the contents of a given property.
||
The logical OR (||) operator works the opposite of the && operator.
It looks for any value in a condition that results in Truthy and takes that value.
Only if both values are Falsy, the outcome will be false:
|| will evaluate all values in a condition until it finds the first
Truthy value. Otherwise, it will take the last value.
Searching for a selection of items
The || operator can be used to filter items that meet one condition or another.
Set default values
If one expression turns out Falsy, you can use the || operator to set up a default value:
Multiple Operators
When you have multiple && &
|| operators in a single statement,
the rule of precedence applies. The first two conditions will be evaluated
and then the outcome of that against the third value;
The ES2020 introduced a new operator, the Nullish Coalescing,
also known as the Elvis operator.
It works similarly to the || operator. However, while || applies to all falsy values,
the Nullish Coalescing operator is limited to
only null and
undefined values.
If used against other types, it will just return the first value:
Another way to avoid nullable properties is to use the Optional Chaining operator.
Instead of repeating yourself:
You can apply the Optional Chaining:
What this will do is:
Evaluate if newArray is nullable
If that's the case, the condition will be evaluated as null/false
which will close the statement (go to the else case).
Check the length of newArray
(after already evaluating newArray)
And it's much cleaner.
Also works for as many properties as you have:
You can combine the Optional Chaining with other operators as well:
The 'Default Username' will print if:
user is null or undefined
user.username is null or undefined
Alternatively, assuming the user property is already defined (has value),
you can assign a default value to user.username
using the Object Destructuring:
Where username will equal to 'Default Username' only if it's undefined or null.
A common inline way to toggle between truthy and falsy values
is to use the Ternary Operator. It works like:
If the condition evaluates to true (a)
The outcome will be the first option (b)
Otherwise, the outcome will be the latter option (c)
The latest addition to JavaScript is a set of Logical Assignment operators.
Think of everything you've learned so far
combined with an ability to assign values on the fly.
So instead of doing:
You'd do:
The motivation behind this is to reduce the extra step of needing
to check values and then assign. Now all of that comes at once.
For other Falsy scenarios, you can use the good old || operator:
This also applies to more complex scenarios:
Implicit Evaluation
If a value evaluates to a truthy or a falsy value, you do not need to
explicitly say that the outcome is either true or false.
#1
# Explicit
# Implicit
#2
# Explicit
# Implicit
#3
# Explicit
# Implicit
Convert to Boolean
You can also explicitly convert a value into a boolean using a pair of
exclamation marks.
Alternatively, you can use the Boolean() constructor: