Rename Variables When Using Object Destructuring in JavaScript
Jun 17, 2022
When destructuring an object, you can rename the variables like so:
const obj = { first_name: 'John', last_name: 'Smith' };
const { first_name: firstName, last_name: lastName } = obj;
firstName; // 'John'
lastName; // 'Smith'
This is perfect for cases where you want to rename variables from a REST API whose naming conventions don't line up with yours.
For example, switching variables from snake_case
to camelCase
or vice versa.
More Fundamentals Tutorials
- How to Add 2 Arrays Together in JavaScript
- The String `match()` Function in JavaScript
- Convert a Set to an Array in JavaScript
- What Does Setting the Length of a JavaScript Array Do?
- Get the Last Element in an Array in JavaScript
- Skip an Index in JavaScript Array map()
- Conditionally Add an Object to an Array in JavaScript