Swapping variables with destructuring assignment

Question | Feb 3, 2016 | hkumar 

I think one of the coolest features introduced to Javascript by ES6 is destructuring assignment. Destructuring is a like a mini query language to extract the fields from arrays and objects to stand-alone variables.

enter image description here

Let's look at some of the examples:

Simple array destructuring:

var [a,b] = [1,2,3,4]; 
console.log(a);   // 1
console.log(b);   // 2

Can be used after variables are declared:

var i = 10;
var j = 25;
[i,j] = [2,45]; // i => 2, j => 45

Destructuring an object:

var { a: x , b: y } = { a: 10, b: 20 }; 
console.log(x);   // 10
console.log(y);   // 20

A little complex example of nested data:
Extract a field from a specific position in array, and a field from sub-object.

var { a: { ain: [,x] }, b: { bin1: y } = { a: { 
                                                ain: [1,2,3] 
                                              }, 
                                           b: { 
                                                bin1: 'cold', 
                                                bin2: 'hot' 
                                              } 
                                          }; 

console.log(x);   // 2
console.log(y);   // cold

Destructuring assignment has more to it like specifying default values and destructuring function parameters. You can read more about those here.

One of the interesting uses of destructuring is variable swap. Look at this code and choose a correct answer to following question:

var x = 10;
var y = 20;

if( y > x )
  [x, y] = [y, x];

console.log(x,',',y);

What will be the output of above?