Skip to content
Advertisement

Destructuring assignment, but without a pattern

The following appears to be valid JavaScript:

const {} = { foo: 'foo' }

Is there a specific use-case for an empty destructuring assignment pattern (is this the right word?) like this?

Advertisement

Answer

I’d say it’s there for consistency. It’s permissible to destructure an empty property list because, otherwise, they’d have to implement code to spcifically disallow it.

Although it may not appear serve any purpose it’s still perfectly correct; no different really from an unused variable or some other redundancy like that. It might also make the process of refactoring easier: properties can be added/removed to a destructuring pattern without worrying that you have to remove the whole assignment if you hit zero at some point in the middle.

Also, as @Bergi said in their comment, it does at least assert that the right hand side is an object, not null or undefined.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement