Home > Tips & Tricks > Dynamic Property Name in JavaScript

Dynamic Property Name in JavaScript

Yes, you can assign the dynamic property name in javascript and access it. You have to declare a variable for your property key and assign that variable in the javascript object.

Before this new way, we can only create the object and use the square bracket notation to access the property. But now, with this ES6 method, you can directly create the object by holding a variable for the dynamic property name and use it as per your need.

Let’s see this new ES6 method:

So, as you can see in the example above, I declare the javascript variable for laptop “Model” and then create the object and use that dynamic variable as a property name holder.

When we console this object, the output will look like this.

{ Laptop: "HP 14", Model: "1005G1" }

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment