Author -  Sai gowtham

Es6 | Destructuring Objects and arrays in JavaScript with examples

In this tutorial, we will learn about how destructuring works in objects and arrays in JavaScript.

What is Destructuring?

Destructuring syntax helps us to extract the values from the objects or arrays into its own variables.

Let’s see examples.

Destrcuturing in JavaScript objects

Without destructuring


let user = {name:"reactgo",age:7}

console.log(user.name,user.age)

We normally get the data from the objects by using dot notation user.name but often we need to write the repeatable code like in the above code we used user.name and user.age where the user is repeated twice.

With destructuring


let user  = {name:"reactgo",age:7}
const {name,age} = name;

console.log(name,age)

In the second line, we have written destructuring syntax the left-hand side we need to define which property values we need to extract from the object insidde the curly braces and right-hand side is from which object you need to extract the properties.

Nested destructuring in objects

sometimes we have complex objects like one object is nested inside the other object


const user = {
    name:"reactgo",
    info:{
      number:839303,
      age:12,
      school:"frontend"
    }
}

// accessing the school property in the object by using dot notation

console.log(user.info.school) // "frontend"


//accessing the school property using nested destructuring
const {info:{school} } = user

console.log(school)

Here we extracted the value of school property from the user object by using nested destructuring syntax {info:{school}} = user.

Destructuring in Arrays

We can also extract the values into its own variables from the arrays also by using destructuring.

array destructuring example


const numbers  = [1,2,3];
const [firstNum,secondNum] = numbers

console.log(firstNum) // 1

console.log(secondNum) // 2

In the above code, we only need the first two numbers from the numbers array so that we only extracted the two numbers into its own variables(firstNum ,secondNum).

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY