Last updated: Feb 28, 2024
Reading timeยท3 min
Use the logical nullish assignment operator to set a variable's value if it's
equal to null
or undefined
.
The logical nullish assignment (??=) operator assigns the provided value to
the variable if it's equal to null
or undefined
.
let role: string | null | undefined = null; role ??= 'developer'; console.log(role); // ๐๏ธ "developer"
We used the
logical nullish assignment (??=)
operator to assign a value to the role
variable if it stores a null
or
undefined
value.
If the value of the role
variable is not equal to null
or undefined
, the
logical nullish assignment (??=) operator short-circuits and doesn't assign the
value to the variable.
let role: string | null | undefined = 'accountant'; role ??= 'developer'; console.log(role); // ๐๏ธ accountant
let
keyword to declare the variable. Variables declared using const
cannot be reassigned.An alternative approach is to use the nullish coalescing (??) operator.
Use the nullish coalescing operator (??) to set a default value if null or undefined in TypeScript.
let role: string | null | undefined = null; role = role ?? 'developer'; console.log(role); // ๐๏ธ "developer"
An easy way to think about the
nullish coalescing operator (??)
is that it allows us to provide a fallback value if the value to the left is
equal to null
or undefined
.
If the value to the left isn't null
or undefined
, the operator returns the
value as is.
const result = 'accountant' ?? 'developer'; console.log(result); // ๐๏ธ "accountant"
Otherwise, the variable gets assigned a new value.
An alternative approach is to explicitly check whether the value is equal to
null
or undefined
with the ternary operator.
let role: string | null | undefined = undefined; role = role == null ? 'tester' : role; console.log(role); // ๐๏ธ "tester"
The ternary operator is
very similar to an if/else
statement.
If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.
role
variable stores a null
or an undefined
value, the ternary operator returns tester
, otherwise, the value stored in the role
variable is returned.You can imagine that the value before the colon is the if
block and the value
after the colon is the else
block.
Notice that we used the loose (==) equality operator in the example.
The loose (==) equality operator checks for both null and undefined.
An easy way to visualize this is to use the operators to compare null
and
undefined
.
console.log(null == undefined); // ๐๏ธ true console.log(null === undefined); // ๐๏ธ false
The example shows that when using the loose equality operator (==), null
is
equal to undefined
.
if
You can also set a default value to a variable if it's equal to null
or
undefined
in a simple if
statement.
let myVar: string | null | undefined = undefined; if (myVar === undefined || myVar === null) { myVar = 'updated value'; } // ๐๏ธ "updated value" console.log(myVar);
We used the logical OR (||) operator to chain 2 conditions.
If either of the conditions returns a truthy value, the if
block runs.
We check if the myVar
variable is equal to null
or undefined
and if it is,
we reassign the variable.
You can also use the logical OR (||) operator to provide a default value if a
variable is null
or undefined
.
const role: string | null = null; const result = role || 'designer'; console.log(result); // ๐๏ธ "designer"
The logical OR (||) operator returns the value to the right if the value to the left is falsy.
This is different from the nullish coalescing operator we used in the first example.
The logical OR (||) operator checks whether a value is falsy, whereas the
nullish coalescing operator (??) checks whether a value is null
or
undefined
.
The falsy values in JavaScript (and TypeScript) are undefined
, null
, 0
,
false
, ""
(empty string), NaN
(not a number).
This means that the logical OR (||) operator will return the value to the right if the value to the left is any of the aforementioned 6 falsy values.
On the other hand, the nullish coalescing operator will return the value to the
right only if the value to the left is null
or undefined
.