FrontendDeveloper.in

ECMAScript question detail

Destructuring

Destructuring is a javascript expression for extracting multiple values from data stored in objects(properties of an object) and Arrays.

Object destructuring:

This feature is used to extract values from an object.

const user = { firstName: 'John', lastName: 'Kary' };
const {firstName, lastName} = user;
console.log(firstName, lastName); // John, Kary

Array destructuring:

This feature is used to extract values from an array.

const [one, two, three] = ['one', 'two', 'three'];
console.log(one, two, three); // one, two, three

You can use destructing in below places,

  1. Variable declarations
  2. Assignments
  3. Parameter definitions
  4. for-of loop
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399