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.
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.
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.
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.
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.
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.
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.definePropertywhen ordinary bracket assignment is already enough. - Forgetting that computed property names are available directly inside object literals.
- Using plain objects where a
Mapwould 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.definePropertywhen you need descriptor control. - Validate external keys and consider
Mapwhen the key space is heavily dynamic.
Related reading
- Is it possible to create a mobile agent that uses Node.js?
- Is it possible to include one CSS file in another?
- Is it possible to trigger share menu on smartphones via HTML/JS?
- Is it possible to use async/await for Publishing a message to RabbitMQ?
- Is it safe to use async with external js files?
- Is the VC code DOM accessible from VS addons?
- Is there a better way to create asynchronous updates without using setInterval?
- Is there a better way to do optional function parameters in JavaScript?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.