Open Playground
JS TutorialLesson #111

JavaScript Destructuring

The destructuring assignment syntax can unpack objects into variables:

Destructuring Assignment Syntax

The destructuring assignment syntax can unpack objects into variables:

let {firstName, lastName} = person;

Object Destructuring

Example

Example Code

javascript
Try it Yourself
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};

// Destructuring

let {firstName, lastName} = person;

The order of the properties does not matter:

Example

Example Code

javascript
Try it Yourself
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};

// Destructuring

let {lastName, firstName} = person;

Destructuring is not destructive. > Destructuring does not change the original object.

Object Default Values

For potentially missing properties we can set default values:

Example

Example Code

javascript
Try it Yourself
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};

// Destructuring

let {firstName, lastName, country = "US"} = person;

Object Property Alias

Example

Example Code

javascript
Try it Yourself
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};

// Destructuring

let {lastName : name} = person;

String Destructuring

One use for destructuring is unpacking string characters.

Example

Example Code

javascript
Try it Yourself
// Create a String

let name = "EA Academy";

// Destructuring

let [a1, a2, a3, a4, a5] = name;
Destructuring can be used with any iterables.

Array Destructuring

We can pick up array variables into our own variables:

Example

Example Code

javascript
Try it Yourself
// Create an Array

const fruits = ["Bananas", "Oranges", "Apples", "Mangos"];

// Destructuring

let [fruit1, fruit2] = fruits;

Skipping Array Values

We can skip array values using two or more commas:

Example

Example Code

javascript
Try it Yourself
// Create an Array

const fruits = ["Bananas", "Oranges", "Apples", "Mangos"];

// Destructuring

let [fruit1,,,fruit2] = fruits;

Array Position Values

We can pick up values from specific index locations of an array:

Example

Example Code

javascript
Try it Yourself
// Create an Array

const fruits = ["Bananas", "Oranges", "Apples", "Mangos"];

// Destructuring

let {[0]:fruit1 ,[1]:fruit2} = fruits;

The Rest Property

You can end a destructuring syntax with a rest property.

This syntax will store all remaining values into a new array:

Example

Example Code

javascript
Try it Yourself
// Create an Array

const numbers = [10, 20, 30, 40, 50, 60, 70];

// Destructuring

const [a,b, ...rest] = numbers

Destructuring Maps

Example

Example Code

javascript
Try it Yourself
// Create a Map

const fruits = new Map([

  ["apples", 500],

  ["bananas", 300],

  ["oranges", 200]

]);

// Destructuring

let text = "";

for (const [key, value] of fruits) {

  text += key + " is " + value;

}

Swapping JavaScript Variables

You can swap the values of two variables using a destructuring assignment:

Example

Example Code

javascript
Try it Yourself
let firstName = "John";

let lastName = "Doe";

// Destructuring

[firstName, lastName] = [lastName, firstName];


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};



// Destructuring

let {firstName, lastName} = person;

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};



// Destructuring

let {lastName, firstName} = person;

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};



// Destructuring

let {firstName, lastName, country = "US"} = person;

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create an Object

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50

};



// Destructuring

let {lastName : name} = person;

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a String

let name = "EA Academy";



// Destructuring

let [a1, a2, a3, a4, a5] = name;

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create an Array

const fruits = ["Bananas", "Oranges", "Apples", "Mangos"];



// Destructuring

let [fruit1, fruit2] = fruits;