Learn Js with Ut

Destructuring in Js

September 20, 2020

Destructuring in js πŸ‘ˆ

Destructuring feature introduce in es6. It’s use to unpack array and object into variable.

Let’s jump on the code

1. Array Destructuring [πŸ‰,🍊,🍌]

it’s looks like this 🎬 let [cake,burger,pizza] = [🍰,πŸ”,πŸ•]

For Example:

// Destructuring is the shorter way to write below code:
let names = ["Steave","Roger"];
let firstName = names[0];
let lastName = names[1];

console.log(firstName); //Steave
console.log(lastName);//Roger

// By using destructuring it's become too easy and short
let [firstName,lastName] = names;
console.log(firstName,lastName); // Steave Roger
  • Ignoring elements using commas

For example:

let [one,,three,four] = [1,2,3,4];
// The second element of the array is skipped
// Because there is no any variable define for them
console.log(one,three,four); // 1 3 4
  • Default Values

Default values will appears if the value is not provided.

For Example:

let firstName,secondName;

[firstName = "Natasha",secondName = "Pepper Potts"] = ["Hulk","IronMan"];
//In above array we have two super hero
// Hulk and Iron Man
// So now firstName have Hulk and secondName have IronMan
// Because both values present into array

console.log(firstName,secondName); // Hulk IronMan

// Now we remove IronMan from above array
[firstName = "Natasha",secondName = "Pepper Potts"] = ["Hulk"];

// Now secondName have Pepper Potts as default value
console.log(firstName,secondName); // Hulk Pepper Potts
  • Destructuring in nested array

For Example:

let randomArray = [

  ["cake","donut",["Orio"]],
  ["Kitkat"]

];

// first we get the cake
let [[cake]] = randomArray;
console.log(cake); // cake
// get the orio
let [[,,[orio]]] = randomArray;
console.log(orio); // Orio
// get the kitkat
let [,[kitkat]] = randomArray;
console.log(kitkat); // "Kitkat"
  • swap variable

For Example:

let firstName = "utkarsh";
let lastName = "srivastava";

// now let swap with destructuring
[firstName,lastName] = [lastName,firstName];

console.log(firstName,lastName); // srivastava utkarsh
  • some more example

For Example:

// split
let [firstName,lastName] = "Steave roger".split(" ");
console.log(firstName,lastName); // Steave roger
// object.entries()
let info = {
  name:"Jake",
  age:23,
};

// here we used destructuring
for(let [key,value] of Object.entries(info)){
  console.log(key,value); // name Jake
                          // age 23
}

// with set
let [one,two,three] = new Set([1,2,3]);
console.log(one,two,three);

// Rest parameter
let [first,...rest] = "I write code".split(" ");
console.log(first,rest); // I ["write","code"]

2. Object Destructuring

In object destructuring we unpack object on the basis of there keys.

Let’s wrap it faster

For Example:

let user = {
  name:"John doe",
  location:{
    lat:78.4665,
    lng:47.123190
  };
  extra:true,
  items:[🍰,πŸ–]
};

// First we see the simple case
// Get the value of name
let {name} = user;
console.log(name); // "John doe"
// we can also assign another name to the object property
let {name:N}  = user;
console.log(N); // "John doe"

// Default value in destructuring
// age does not exit inside user so i will give default value
let {age = 23} = user;
console.log(age); // 23
  • Nested destructuring

let user = {
  name:"John doe",
  location:{
    lat:78.4665,
    lng:47.123190
  };
  extra:true,
  items:[🍰,πŸ–]
};

// get the lat lng
let {location:{lat,lng}} = user;
console.log(lat,lng); // 78.4665 47.123190
// get the cake and meat
let {items:[cake,meat]} = user;
console.log(cake,meat); // 🍰 πŸ–

I think that’s it for destructuring



Please subscribe our news letter

If you like what you just read. Please subscribe to our newsletter. So new content will be delivered to your inbox :)

Hii this is Utkarsh Srivastava. I live in India. I
Coding, Learning New Tech, and Playing PC Games. You can find me on
Β© 2020, Built with