How to set default value in JavaScript’s Destructuring

Article autor
September 9, 2025
How to set default value in JavaScript’s Destructuring
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

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

No items found.

You might also like

Discover more content from this category

How to create and use custom shell commands?

Each of us had a situation, where we had to invoke a few, same commands each time. Making it once is not that problematic, but when we are supposed to repeat given operations, it may be better just to create one function that will handle it all.

How to revert commit in Git

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.

How to change column to nullable with modify in Ecto migration

Sooner or later you'll have to change the null constraint in one of your DB relations. How to do it easily in Ecto?