var arr = ["Hello", "World"] // destructuring assignment var [first, second] = arr; console.log(first); // Hello console.log(second); // World
Hello World
var colors = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"]; // destructuring assignment var[color1, color2, color3] = colors; console.log(color1); // Violet console.log(color2); // Indigo console.log(color3); // Blue
Violet Indigo Blue
var colors = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"]; // destructuring assignment var[color1, ,color3, ,color5] = colors; //Leave space for unpick elements console.log(color1); // Violet console.log(color3); // Blue console.log(color5); // Yellow
Violet Blue Yellow
var colors = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"]; // destructuring assignment var [a,b,...args] = colors; console.log(a); console.log(b); console.log(args);
Violet Indigo [ 'Blue', 'Green', 'Yellow', 'Orange', 'Red' ]
var x, y; [x=50, y=70] = [100]; console.log(x); // 100 console.log(y); // 70
100 70
var x = 100, y = 200; [x, y] = [y, x]; console.log(x); // 200 console.log(y); // 100
200 100
function array() { return [100, 200, 300]; } var [x, y, z] = array(); console.log(x); // 100 console.log(y); // 200 console.log(z); // 300
100 200 300