How to set default value in JavaScript’s Destructuring

Did you know that it's possible to set default value in Javascript object destructuring?
Let see how it works in action:
> { firstName = "John" } = {}
{}
> firstName
'John'
One thing to keep in mind is that it only works with undefined
values. It won't work with null
, false
and 0
as these are normal values.
> { firstName = "John" } = { firstName: null }
{ firstName: null }
> { firstName = "John" } = { firstName: 0 }
{ firstName: 0 }
> {firstName = "John"} = { firstName: false }
{ firstName: false }
Related posts
Dive deeper into this topic with these related posts
You might also like
Discover more content from this category
Nowadays, with an ever-growing number of web services, we tend to overload Web apps with external resources. As a result, it decreases page load speed and affects SEO score. There is a pretty easy solution for that.
Ensuring that GraphQL mutations properly update your Apollo client's cache can be a bit tricky - here's how to manually control that.
Learn a trick that will allow you to manage item order in Postgres tables easier & faster.