const test = () => {
const arr = [1, 2]
console.log("any strings") // focus on this line
[arr[0], arr[1]] = [arr[1], arr[0]]
}
const test1 = () => {
const arr = [1, 2]
console.log("any strings"); // add a semicolon will works or comment this line will works too
[arr[0], arr[1]] = [arr[1], arr[0]]
}
test() // error
test1() // works
Why does the first function test throw error "Cannot set property '2' of undefined"?
possibleneeded, you will [need to put one at the *begin*](https://web.archive.org/web/20201206065632/http://inimino.org/~inimino/blog/javascript_semicolons) of every line that starts with `(`, `[`, `/`, `+`, `-` or `\``. – Bergi Aug 15 '21 at 17:34