Categories
JavaScript Answers

How to get value from object with default value with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.