-1

I'm stuck at this task. How can I use array destructuring to change the three statements below into 1 statement?

The main reason for my confusion is that a and b are already declared. And there is no array.

´´´

(function UseArrayDestructuring2() {
        let a = 1;
        let b = 2;
        
        // Use array destructuring to change the 3 statements below into 1 statement.
        // You should not need a temporary variable anymore.
        let tmp = a;
        a = b;
        b = tmp; 

        // Don't make changes below this line   
        
        expect(a).toEqual(2);
        expect(b).toEqual(1);

´´´

KarthikNayak98
  • 363
  • 3
  • 13
Tore
  • 99
  • 10
  • 1
    You can *make* an array then destructure it. – jonrsharpe Feb 16 '21 at 08:20
  • 1
    This looks like a code assessment... – Martijn Feb 16 '21 at 08:22
  • 1
    Does this answer your question? [How to swap two variables in JavaScript](https://stackoverflow.com/questions/16201656/how-to-swap-two-variables-in-javascript) ([This answer](https://stackoverflow.com/a/25910841/479156) specifically.) – Ivar Feb 16 '21 at 08:22

1 Answers1

4

You could collect the values in an array and destructure the array to the swiched variables.

This approach creates still a temporary value.

[b, a] = [a, b];
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392