Sometimes, we want to get value from object with default value with JavaScript.
In this article, we’ll look at how to get value from object with default value with JavaScript.
How to get value from object with default value with JavaScript?
To get value from object with default value with JavaScript, we can use the null coalescing operator.
For instance, we write
let foo = obj.maybeUndefined ?? "default value";
to use 'default value'
as the fallback value for foo
if obj.maybeUndefined
is null
or undefined
.
The null coalescing operator is ??
.
Conclusion
To get value from object with default value with JavaScript, we can use the null coalescing operator.