DEV Community

Sujith V S
Sujith V S

Posted on

Destructuring in JavaScript | ES6

Destructuring is used to extract the array elements or object properties and store them in variables.
See the example below,

Object destructuring

//Array destructuring
const number = [1, 2, 3];

const [num1, , num3] = number
console.log(num1, num3)

Output:
1 3
Enter fullscreen mode Exit fullscreen mode

Array destructuring

//Object destructuring
const myFriends = {
    friend1:'Nivedh',
    friend2:'Vibin',
    friend3:'Joel'
}
const {friend2, friend1} = myFriends
console.log(friend1, friend2)

Output:
Nivedh Vibin
Enter fullscreen mode Exit fullscreen mode

Top comments (0)