Sign in

Array Destructuring in JavaScript

[var1,var2,var3] = array

image of javascript

Let us Understand the use of array destructuring in ES6

In ES5 if the array elements were to be named with a variable instead of accessing them with their index we were supposed to initialize them with variable

Let us take an Example

var arr = [ "one" , "two" , "three" ]

like this, we were supposed to access the array elements but with array destructuring, this can be skipped

var arr = [ "one" , "two" , "three" ]

What if we don't want to assign variables to all the array elements in such case what can be done

var arr = [1,2,3,4,5]

EXERCISE — — — — — — — — — — — — — — —

SWAP TWO NUMBERS WITHOUT USING A TEMPORARY VARIABLE

let a = 10 ;
let b = 20 ;