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
It's a pretty common scenario - you have to place a few elements in equal distances. E.g. unordered list items.
Warnings in Elixir are usually an important sign of a problem in the codebase. There is an easy way to make them gone.
There are a bunch of operations you may want to perform before the rendered response in conn is sent to the client, such as minification. In this post I'll show you how to do it easily.
