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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *