ES6 Array destructuring weirdness
As others have said, you're missing semicolons. But…
Can anyone explain?
There are no semicolons automatically inserted between your lines to separate the "two" statements, because it is valid as a single statement. It is parsed (and evaluated) as
let a = undefined, b = undefined, c = undefined;
[a, b] = (['A', 'B']
[(b, c)] = ['BB', 'C']);
console.log(`a=${a} b=${b} c=${c}`);
wherein
[a, b] = …;
is a destructuring assignment as expected(… = ['BB', 'C'])
is an assignment expression assigning the array to the left hand side, and evaluating to the array['A', 'B'][…]
is a property reference on an array literal(b, c)
is using the comma operator, evaluating toc
(which isundefined
)
If you want to omit semicolons and let them be automatically inserted where ever possible needed, you will need to put one at the start of every line that begins with (
, [
, /
, +
, -
or `
.
Javascript array destructuring gives error when chaining
You need some semicolons to prevent using the brackets as property accessor, because the ASI (Automatic Semicolon Insertation) does not work here.
If you write this in a line, you see it directly:
console.log(`min: ${min}`)[hr, min] = integerDivide(min, 60)
The linebreak does not separate the statements, followed by brackets. To overcome this, you need to add the semicolon for separating the statements.
More to read here: What are the rules for JavaScript's automatic semicolon insertion (ASI)?
const integerDivide = (a , b) => {
return [Math.floor(a/b), a%b]
}
let sec = 555003, min, hr, day;
[min, sec] = integerDivide(sec, 60)
console.log (`sec: ${sec}`)
console.log (`min: ${min}`); // <-- here at least
[hr, min] = integerDivide(min, 60)
console.log (`hr: ${hr}`)
Interesting behavior about ES6 array destructuring and swapping
Add a semicolon to the 1st line, because the interpreter assumes that lines 1 and 2 declarations happen together, and one
(and two
) is not defined yet:
let [one, two, three] = [1, 2, 3];[one, two] = [two, one]console.log(one, two)
TypeError when swapping object properties with destructuring assignment
The syntax is fine. Add a semicolon to prevent the automatic semicolon insertion from thinking you want to do console.log(...)[...]
instead of array destructuring:
let dataObj = {"reg_price":2, "reg_price_alt":5, "ex":9}
console.log("before: ", dataObj); // <-- semicolon
[dataObj.reg_price, dataObj.ex] = [4, 5]
console.log("after: ", dataObj)
Destructuring array into an object
Try this:
let res = { start: {}, end: {}};[res.start.hour, res.start.minute] = [7, 20]; // Notice the semicolons.[res.end.hour, res.end.minute] = [17, 30];console.log(res);
Related Topics
How to Return Values from Async Functions Using Async-Await from Function
Can a PDF File's Print Dialog Be Opened with JavaScript
How to Overload Functions in JavaScript
Load Jquery with JavaScript and Use Jquery
Preventdefault() on an <A> Tag
Turning Off Eslint Rule for a Specific Line
Print Content of JavaScript Object
Scrolling an Iframe with JavaScript
Regular Expression for Password Validation
Create Svg Tag with JavaScript
Returning Chrome Storage API Value Without Function
Onclick Function Runs Automatically
How to Copy Static Files to Build Directory with Webpack