Moderator: Mmiscool
- Destructuring assignment (a feature of ES2015) lets you extract items of an array into variables. For example, the following code destructures an array:
let a;
let b;
[a, b] = [1, 2, 3];
a; // => 1
b; // => 2
- Or Temporary variable: Swapping variables using a temporary variable is classic. As the name suggests, this approach requires an additional temporary variable.Let's swap the values of variables a and b using a temporary variable temp:
let a = 1;
let b = 2;
let temp;
temp = a;
a = b;
b = temp;
a; // => 2
b; // => 1