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 }
Work with a team that keeps learning and building better software every day.
Related posts
Dive deeper into this topic with these related posts
You might also like
Discover more content from this category
Did you ever create a commit that you wish never happened? Let's be honest - we all did. There is an easy way to revert it in Git.
It's a pretty common scenario - you have to place a few elements in equal distances. E.g. unordered list items.
Hey! Have you ever wondered about tests running inside the IEx shell? For a long time, I was convinced that it’s not really possible. And as it turns out - that’s not really straightforward. You won’t easily find information about that in the documentation.
