# How to create an object with dynamic keys in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Mar 29, 2021. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-create-object-with-dynamic-keys

> A quick guide to learn how to create an object with dynamic keys by using computed property names feature in JavaScript.

To [create an object](https://attacomsian.com/blog/javascript-create-object) with dynamic keys in JavaScript, you can use ES6's [computed property names](https://attacomsian.com/blog/javascript-computed-property-names) feature.

The computed property names feature allows us to assign an expression as the property name to an object within object literal notation.

Here is an example:

```javascript
const key = 'title';
const value = 'JavaScript';

const course = {
    [key]: value,
    price: '$99'
};

console.log(course.title);  // JavaScript
console.log(course.price);  // $99 
```

The value of the `key` can be any expression as long as it is wrapped in brackets `[]`:

```javascript
const key = 'title';
const value = 'JavaScript';

const course = {
    [key + '2']: value,
    price: '$99'
};

console.log(course.title2);  // JavaScript
console.log(course.price);  // $99 
```
