JavaScript Fundamentals: Destructuring

What is destructuring? how do we use destructuring? and why is destructuring important for clean code?

JavaScript has some powerful features for working with objects and arrays. Destructuring is a way of making working with complex data structures easier by breaking them down into smaller, more manageable pieces.

In its simplest form, destructuring can be used to extract values from arrays and objects and assign them to variables.

Destructuring allows you to bind a group of variables to a corresponding set of values at once.

Destructuring is important in javascript because it allows you to extract data from arrays and objects and store them in variables. This is especially useful when working with data that is returned from APIs.

For example, consider the following array:

const arr = [1, 2, 3];

We can use destructuring to assign the values of the array to individual variables like so:

const [a, b, c] = arr;

console.log(a); // 1
console.log(b); // 2
console.log(c); // 3

Destructuring is important in JavaScript because it allows you to extract data from arrays and objects and store them in variables. This is especially useful when working with data that is returned from APIs.

Skipping over values in array

We can also skip over values that we don't want to assign to variables through destructuring:

const [a, , c] = arr;

console.log(a); // 1
console.log(c); // 3

Assigning properties of objects to variables

Destructuring can also be used to assign the properties of objects to variables.

For example, consider the following object:

const obj = {
  foo: 'bar',
  baz: 'qux'
};

We can use destructuring to assign the values of the object properties to individual variables like so:

const { foo, baz } = obj;

console.log(foo); // 'bar'
console.log(baz); // 'qux'

Setting new variable names

We can use destructuring to assign the properties of objects to new variable names in javascript. This can be useful when we want to create new variables that have the same names as the properties of an object.

const { foo: f, baz: b } = obj;

console.log(f); // 'bar'
console.log(b); // 'qux'

Using '...rest' for complex data structures

Destructuring can be used in conjunction with rest parameters to make working with complex data structures easier. For example, consider the following array:

const arr = [1, 2, 3, 4, 5];

We can use destructuring to get the first three elements of the array and assign them to variables like so:

const [a, b, c, ...rest] = arr;

console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
console.log(rest); // [4, 5]

Destructuring can be a very useful tool for working with data in JavaScript. It can make working with complex data structures easier by breaking them down into smaller, more manageable pieces.

For-loop destructuring pattern

For-of loops allow you to iterate over the values of an array or iterable object. When used with a destructuring pattern, you can iterate over the values of an array or iterable object and extract the values into individual variables.


const arr = [1, 2, 3, 4];

for (const [index, value] of arr.entries()) {
  console.log(index, value);
}

Destructuring with function parameter list

A destructuring assignment within a function parameter list allows you to easily work with arguments that are passed into the function.

A function parameter list is a list of parameters that a function accepts.

This can be helpful when you want to access specific values from an array or object, or when you want to ensure that certain arguments are passed into the function in a specific order.

Use a destructuring assignment within a function parameter list to make it easy to work with arguments passed into the function.

function foo([x, y]) {
  console.log(x, y);
}

foo([1, 2]); // 1 2

Dealing with return statement objects

A destructuring assignment with a return statement can be used to concisely return multiple values from a function. This is often useful when returning values from an object or array.

function getPersonData() {
  return {
    firstName: "John",
    lastName: "Doe",
    age: 25
  };
}

const { firstName, lastName, age } = getPersonData();

console.log(firstName, lastName, age);

Article by:

Aphinya Dechalert

Aphinya Dechalert

Aphinya is a skilled tech writer with field experience in software development, agile, and fullstack JavaScript. She is a dev advocate and community builder, helping others navigate their dev journey.

More posts...

JavaScript Fundamentals

JavaScript Fundamentals: Fetch

What is fetch()? how did fetch() come about? why is fetch() important?

JavaScript Fundamentals

JavaScript Fundamentals: JSON

What is JSON? How does it work? what are some JSON methods in JavaScript?

JavaScript Fundamentals

JavaScript Fundamentals: Prototypical Inheritance

What is a prototype? How does prototypical inheritance works? What are some examples of prototypes in JavaScript?

JavaScript Fundamentals

JavaScript Fundamentals: Advanced Classes

What are class methods? how do you use class methods? what are some common default class methods you should know about.