0

I have this code:

let [foo, bar] = [0,1];

console.log("*") // If I don't put a semi colon here it fails 
[foo, bar] = d(foo, bar);
console.log(foo, bar)

function d(a, b) {
    return [1+a, 1+b]
}

If I don't add a semicolon at the line with the comment I get:

TypeError: console.log(...) is undefined

Is this a known behaviour of array destructing? (using Firefox)

funerr
  • 7,212
  • 14
  • 81
  • 129
  • 8
    It's a known behavior of *automatic semicolon insertion*. If an expression across a newline boundary makes syntactic sense, the parser assumes it's all the same expression. `console.log("*")[foo, bar]` is not a syntax error, so it assumes that's what you mean. – Pointy Sep 06 '22 at 14:55
  • 5
    The moral of this story is **use semicolons**. – Pointy Sep 06 '22 at 14:56
  • 1
    The answer should be here https://stackoverflow.com/questions/16664824/no-semicolon-before-is-causing-error-in-javascript – Емил Цоков Sep 06 '22 at 15:07

0 Answers0