JS Destructuring

JS Destructuring

·

1 min read

Table of contents

No heading

No headings in the article.

Twitter Youtube

Different ways in array destructuring:

4.png

You can skip items you don't want:

5.png

When you want to assign only few variables and rest as separate:

6.jpeg

You can also set default values:

7.png

assign variables via destructering:

const DAYS = {
  yesterday: 0,
  today: 1,
  tomorrow: 2
};

const {today: todayCount, tomorrow: tomorrowsCount  }  = DAYS;

console.log(todayCount);
> 1
console.log(tomorrowsCount);
> 2

assign variables for nested objects:

const DAYS = {
  yesterday: { low: 0, high: 1 },
  today: { low: 1, high: 2 },
  tomorrow: { low: 2, high: 3 }
};

const {today: {low: lowToday, high: highToday }} = DAYS;

console.log(lowToday);
> 1
console.log(highToday);
> 2

Swap using destructuring:

8.png

We can also destructure nested ones like:

9.png

We can also achieve multi array destrcuturing:

10.jpeg