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 Lazy-Load external scripts for better page speed?

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.

Manually update Apollo cache after GraphQL mutation

Ensuring that GraphQL mutations properly update your Apollo client's cache can be a bit tricky - here's how to manually control that.

Why & when you should use PostgreSQL deferred uniqueness constraints

Learn a trick that will allow you to manage item order in Postgres tables easier & faster.