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); // 10Advertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 60
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
317of476