Node: error in destructuring assignment if no semi colon

Created on 9 Jul 2017  路  5Comments  路  Source: nodejs/node

  • Version: v8.1.3
  • Platform:Linux Mint 17.3 Rosa MATE 64-bit
  • Subsystem:
var a = 1
var b = 3

console.log(a, b)
[a, b] = [b, a]
console.log(a, b)

above code will trigger an error:
[a, b] = [b, a] ^ TypeError: Cannot set property '3' of undefined

putting a semi colon in console.log line solves it:

````
var a = 1
var b = 3

console.log(a, b);
[a, b] = [b, a]
console.log(a, b)
````
is this a bug? thanks

question

All 5 comments

This is just how javascript works (with automatic semicolon insertion), so it's not a bug.

Also, this StackOverflow answer is a (IMO) pretty clear explanation of what's going on: https://stackoverflow.com/a/39748189/436641

@sqllyw U can take a look at this
11.9 Automatic Semicolon Insertion

@sqllyw Your code is actually parsed as:

console.log(a, b)[a, b]

ie

console.log(a, b)[3]

ie

undefined[3]

so this produces the mentioned error message.

Was this page helpful?
0 / 5 - 0 ratings