1

I tried spread in javascript (node and chrome).

thanks to @jonrsharpe, @Quentin show me What are the rules for JavaScript's automatic semicolon insertion (ASI)? explains that semicolon is necessary and that explain why is necessary use a semicolon.

let c, d
[ c, ] = [7, 8]; // <-- semicolon is necessary 
[ , d ] = [7, 8] // Otherwise, here: Uncaught SyntaxError: Unexpected token ,
let c, d
[ , d ] = [7, 8]; // <-- semicolon is necessary 
[ c, ] = [7, 8] // Otherwise, here: Uncaught SyntaxError: Unexpected token ]

Just for curious, I keep tried testing without semicolon and discover this behavior:

let c, d
[ c, ] = [3, 4]
[ c, d ] = [5, 6]
console.log(c) //<-- 5
console.log(d) //<-- undefined

what explains 'd' keeps "undefined"? I hoped at least an error o d with some value.

molavec
  • 8,848
  • 1
  • 27
  • 22
  • Apparently, it is necessary use semicolon in line before use destructuring to avoid unexpected behaviour. Otherwise, javascript consider variables or values as position in array. – molavec Mar 31 '19 at 15:34

0 Answers0