Why does object destructuring throw an error if there is no var
keyword in front of it?
{a, b} = {a: 1, b: 2};
throws Sy
If you write Javascript without semicolons, then the 'assignment without declaration' syntax should be preceded with a semicolon for it to work predictably
let a, b
;({a, b} = objectReturningFunction()) // <-- note the preceding ;
Just wanted to highlight this as it caught me out, and hopefully can save others some time figuring out why it doesn't work and/or produces weird results with code formatters like prettier
.
Indeed, it's actually right there in the accepted answer (last line of the quoted docs) but easy to miss, especially without seeing an example!