Deconstructing


var myArray = [10, 20, 30, 40]; 
var item1 = myArray[0];
var item2 = myArray[1];
var item3 = myArray[2];
var item4 = myArray[3];

Alternative syntax

var [item1, item2, item3, item4] = myArray; 

It can even work on multiple lines
It can even work with a comma after item4

var [ 
   item1,
   item2,
   item3,
   item4,
] = myArray

You can even use an array containing all the other values

var [ 
   item1,
   item2,
   ...moreArgs
] = myArray

You can even add default values

var [ 
   item1,
   item2 = true,
   ...moreArgs
] = myArray


before

let myArray = ['one', 'two', 'three'];  
let e1 = myArray[0];
let e2 = myArray[1];
let e3 = myArray[2];

after

let e1, e2, e3 
[e1, e2, e3] = [ ];



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext