Skip to content
Advertisement

TypeError when swapping object properties with destructuring assignment [duplicate]

I am trying to swap two object values in JavaScript using the [] = [] method, but my below code fails with an error saying "message": "Uncaught TypeError: Cannot set property '9' of undefined",

JavaScript

Is there some syntax I’m missing? I don’t understand why this simple code does not work.

Advertisement

Answer

The syntax is fine. Add a semicolon to prevent the automatic semicolon insertion from thinking you want to do console.log(...)[...] instead of array destructuring:

JavaScript

I’d take this a step further and add semicolons after every line. Caveat emptor otherwise. Example of swapping values:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement