JavaScript
Programming
Web Development
Dynamic Properties
Object-Oriented Programming

Is it possible to add dynamically named properties to JavaScript object?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Yes. JavaScript objects allow property names to be determined at runtime, which is one of the language’s most flexible features. The usual tools are bracket notation for assignment after object creation and computed property names when you want the dynamic key during object literal construction.

Add Dynamic Properties with Bracket Notation

The most direct technique is bracket notation.

javascript
1const user = {
2  name: 'John',
3  age: 30
4};
5
6const propertyName = 'email';
7user[propertyName] = '[email protected]';
8
9console.log(user.email);

Bracket notation works because the expression inside the brackets is evaluated first, then used as the property name. Dot notation cannot do that because user.propertyName looks for the literal key propertyName.

Use Computed Property Names in Object Literals

If you want the key to be dynamic at creation time, use computed property names.

javascript
1const field = 'email';
2
3const user = {
4  name: 'John',
5  [field]: '[email protected]'
6};
7
8console.log(user.email);

This is especially useful when building objects from configuration or from transformed data.

Use Object.defineProperty for Descriptor Control

Sometimes the property is dynamic and you also care about enumerability, writability, or configurability. In that case, Object.defineProperty is the better tool.

javascript
1const user = { name: 'John' };
2const key = 'email';
3
4Object.defineProperty(user, key, {
5  value: '[email protected]',
6  writable: true,
7  enumerable: true,
8  configurable: true
9});
10
11console.log(user.email);

This is not needed for ordinary assignment, but it is useful when object shape and behavior matter.

Build Objects from Dynamic Inputs Safely

Dynamic property names are common when normalizing API data, reading form inputs, or generating configuration maps.

javascript
1const pairs = [
2  ['host', 'localhost'],
3  ['port', 5432],
4  ['ssl', false]
5];
6
7const config = {};
8for (const [key, value] of pairs) {
9  config[key] = value;
10}
11
12console.log(config);

That pattern is simple, readable, and often better than trying to predeclare every possible property.

Know When an Object Is the Wrong Structure

If the keys are truly dynamic and may collide with built-in object semantics, a Map can be a better choice than a plain object. Objects are fine for most application records, but Map is often better when keys are heavily dynamic or not always strings.

javascript
1const settings = new Map();
2settings.set('theme', 'dark');
3settings.set('pageSize', 50);
4console.log(settings.get('theme'));

The answer to the title question is still yes, but the design question may be whether an object is the right container.

Be Careful with Untrusted Keys

Dynamic keys are powerful, but they can become dangerous if they come from untrusted input. Blindly writing user-supplied keys into plain objects can create maintenance problems and, in some contexts, security issues such as prototype pollution.

Whenever keys come from external input, validate or constrain them before assignment.

In day-to-day code, bracket notation remains the default because it is short, direct, and understood by every JavaScript developer immediately.

Common Pitfalls

  • Using dot notation when the goal is to evaluate a variable as the property name.
  • Reaching for Object.defineProperty when ordinary bracket assignment is already enough.
  • Forgetting that computed property names are available directly inside object literals.
  • Using plain objects where a Map would better express highly dynamic key usage.
  • Writing unvalidated external keys into objects without thinking about safety or object shape.

Summary

  • JavaScript supports dynamically named properties directly.
  • Use bracket notation for runtime assignment after object creation.
  • Use computed property names for dynamic keys in object literals.
  • Use Object.defineProperty when you need descriptor control.
  • Validate external keys and consider Map when the key space is heavily dynamic.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.