JS Coding Questions Logo
JS Coding Questions
#317💼 Interview💻 Code

How do you swap variables in destructuring assignment

Advertisement

728x90

If you don't use destructuring assignment, swapping two values requires a temporary variable. Whereas using a destructuring feature, two variable values can be swapped in one destructuring expression. Let's swap two number variables in array destructuring assignment,

javascript
1var x = 10,
2  y = 20;
3
4  [x, y] = [y, x];
5
6console.log(x); // 20
7
8console.log(y); // 10

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 60

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
317of476
How do you swap variables in destructuring assignment | JSCodingQuestions.com