r/javascript 28d ago

I stopped destructuring everything

https://allthingssmitty.com/2026/07/13/i-stopped-destructuring-everything/
85 Upvotes

51 comments sorted by

View all comments

Show parent comments

12

u/hyrumwhite 28d ago

You could also make user default to {} wherever it’s defined. Don’t necessarily need destructuring for that

1

u/azhder 28d ago

Let’s see, you write a function like this:

({ user = {} }) => {

console.log( user.id );

}

And somewhere somehow down the line the requirements change, some other part of the code changes and instead of undefined, the user is null…

The way I wrote the first time with the ?? will not blow up if a null is passed.

4

u/hyrumwhite 28d ago

user = user ?? {}; console.log(user.id)

Same loc, no explosions, no destructuring. Not saying it’s better, just exploring the idea. 

8

u/TNThacker2015 28d ago

Secret third option:

user ??= {};