10 minutes read

Javascript Rename Variables at Object Destructuring

Summary:This post explains how we can rename variables while using the object destructuring in Javascript.

Destructuring in Javascript helps to unpack values from arrays and properties from objects into the individual variables.

During destructuring an object sometimes there is a need to change the variable name of the fields that we unpack.

The reason may be we have already declared a variable with the same name or anything else.

Here is a simple code snippet that demonstrates how to rename variables while using object destructuring in Javascript.

const student = {
  firstName: 'Digvijay',
  lastName: 'Singh',
}

//Rename firstName -> name
const {firstName: name, lastName} = student

console.log(name) //Digvijay
console.log(lastName) //Singh

In the above code snippet we renamed the firstName variable to the name during the object destructuring.

Join Our Youtube Channel

Subscribe