Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 585 Bytes

File metadata and controls

20 lines (16 loc) · 585 Bytes

Today I learned a straight-forward way to swap the values of two variables in JavaScript.

Say var a = 10 and var b = 20.

Previously, I would use the following (tedious) strategy to swap the values:

var temp = a;  // temp is now 10
a = b;         // a is now 20
b = temp;      // b is now 10

A quicker way to do this is:

b = [a, a = b][0];

Finally, in ECMAScript 6 you can use destructuring assignment:

[a, b] = [b, a];