0

when i am taking values into x and y using destructuring its working. but when I am trying to swap the values using Destructuring its simply showing Typeerror. I have searched this problem on the internet and found no solution any help would be appreciatable.

const arr=new Array(2,3,4);
let [x,y,z]=arr;
console.log(x,y)
let a="23";

let b="20";
console.log(a,b)

[a , b] = [b , a];
console.log(a,b)

// [x,y]=[y,x];
console.log(x,y)

i am expecting the values of a and b to be sawpped.

  • Use semicolons. `console.log(a, b)` `[a, b] = [ b, a ];` means `console.log(a, b)[a, b] = [ b, a ];` which means `undefined[b] = [ b, a ];`. – Sebastian Simon Dec 16 '22 at 19:30

0 Answers0