-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);

´´´

3
  • 1
    You can make an array then destructure it. Commented Feb 16, 2021 at 8:20
  • 1
    This looks like a code assessment... Commented Feb 16, 2021 at 8:22
  • 1
    Does this answer your question? How to swap two variables in JavaScript (This answer specifically.) Commented Feb 16, 2021 at 8:22

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

It creates a temporary value, the array, but no additional variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.