Home » Javascript » js strange behavior Array Destructuring

js strange behavior Array Destructuring

Posted by: admin June 30, 2018 Leave a comment

Questions:
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
Answers: