Skip to content

Aliasing object properties when destructuring

Published: at 05:28 PM

I’ve been working my way through The Joy of React, an exhaustive guide to React written by Josh Comeau. In one of the sections I came across destructuring syntax that I was unfamiliar with. The syntax allows you to alias object properties when destructuring. Here’s an example:

const user = { name: "Ras", age: 30 };

// Destructure the user object and alias the name property to firstName
const { name: firstName, age } = user;

This is probably one of those things that everyone else knows about but I had just never run across. I use destructuring a good bit in my day-to-day work, but I had never seen this syntax before. I’m not sure how I missed it, but I’m glad I found it. It’s a great way to alias object properties when destructuring.