published: 27 Aug 2022
2 min read
How to use computed property names in JavaScript
The computed property names feature was introduced in ECMAScript 2015(ES6) that allows you to dynamically compute the names of the object properties in JavaScript object literal notation.
A JavaScript object is just a collection of key-value pairs called properties. A property's key is a string or symbol (also known as property name), and value can be anything.
Before computed property names, if you want to create an object with dynamic property names, you'd have to create the object first and then use bracket notation to assign that property to the value:
const key = 'name';
const value = 'Atta';
const user = {};
user[key] = value;
console.log(user.name); // Atta
However, ES6 computed property names feature allows you to assign an expression as the property name to an object within object literal notation. There is no need to create an object first.
The above code can now be rewritten like below:
const key = 'name';
const value = 'Atta';
const user = {
[key]: value
};
console.log(user.name); // Atta
The key
can be any expression as long as it is wrapped in brackets []
:
const key = 'name';
const value = 'Atta';
const user = {
[key + '34']: value
};
console.log(user.name34); // Atta
One last thing, you can also use template literals (string interpolation) as an expression for the computed property names:
const key = 'name';
const value = 'Atta';
const user = {
['${key}34']: value
};
console.log(user.name34); // Atta
Are we missing something? Help us improve this article. Reach out to us.
How to use computed property names in JavaScript
The computed property names feature was introduced in ECMAScript 2015(ES6) that allows you to dynamically compute the names of the object properties in JavaScript object literal notation.
A JavaScript object is just a collection of key-value pairs called properties. A property's key is a string or symbol (also known as property name), and value can be anything.
Before computed property names, if you want to create an object with dynamic property names, you'd have to create the object first and then use bracket notation to assign that property to the value:
const key = 'name';
const value = 'Atta';
const user = {};
user[key] = value;
console.log(user.name); // Atta
However, ES6 computed property names feature allows you to assign an expression as the property name to an object within object literal notation. There is no need to create an object first.
The above code can now be rewritten like below:
const key = 'name';
const value = 'Atta';
const user = {
[key]: value
};
console.log(user.name); // Atta
The key
can be any expression as long as it is wrapped in brackets []
:
const key = 'name';
const value = 'Atta';
const user = {
[key + '34']: value
};
console.log(user.name34); // Atta
One last thing, you can also use template literals (string interpolation) as an expression for the computed property names:
const key = 'name';
const value = 'Atta';
const user = {
['${key}34']: value
};
console.log(user.name34); // Atta
Are you looking for other code tips?
JS Nooby
Javascript connoisseur