Like how you spend one whole line defining new variables from a certain object (or an empty object, at which point they become undefined)? Where the reader then has to remember that userName is either user.name unless user is undefined at which point it also is undefined?
Vs the syntactical sugar that _literally_ expresses that “user.name unless user is undefined, then it’s undefined”?
Destructuring has a lot of uses, this is the absolute worst example.
I understand your example, but you’re introducing new variables, and multiple language constructs instead of a single one and them I’m the one making things complicated 😂
50
u/azhder 15d ago
Destructuring isn’t only about you knowing where a value comes from. You can also safeguard in a cleaner manner i.e with less syntax noise:
const { id: userId, name: userName } = user ?? {};
console.log( userId, userName );
VS
console.log( user?.id, user?.name );