Rename variable when destructuring

Last update in 9/2019

One of my favorite ES6 additions to JavaScript is array and object destructuring. And one of great things you might not know about it is that you can rename destructured variable.

Let's say you have JSON data similar to this...

"firstName": "Bartol",
"lastName": "Deak"

...and you are destructuring it, but one variable is conflicting with other. Rather than changing whole code, you can assign different name for destructured variable.

const { firstName: fName, lastName } = data

And that's all there is to it!