How to Set Default Value If Null in Javascript
Posted on: February 17, 2021 by Ariessa Norramli
In this article, you will learn how to set default value if null in Javascript.
Let’s say you have a null variable named ‘a’ with the value null.
var a = null;
In order to set a default value, you can use the nullish coalescing operator ??
.
var a = null;
console.log(a ?? "a is null");
Note: The nullish coalescing operator ??
is a logical operator that functions by returning the value on its right side if the value on its left side is null
or undefined
. Else, it will return the value on its left side.
Share on social media
//