This works for me
function addProperty(object, property) {
// add the property to the object with a value of null
// return the object
// note: the property name is NOT 'property'. The name is the value
// object.property = property;
object[property] = null;
return object;
}
var obj = {x:1,y:null};
// undefined
obj
// {x: 1, y: null}
addProperty(obj, 'z');
// {x: 1, y: null, z: null}
Answer from Niccaman on Stack Overflow Top answer 1 of 4
3
This works for me
function addProperty(object, property) {
// add the property to the object with a value of null
// return the object
// note: the property name is NOT 'property'. The name is the value
// object.property = property;
object[property] = null;
return object;
}
var obj = {x:1,y:null};
// undefined
obj
// {x: 1, y: null}
addProperty(obj, 'z');
// {x: 1, y: null, z: null}
2 of 4
2
The preferred way of doing this if you ask me is to use Object.assign. This will copy all the properties of the second object you pass in into the first object. You can also send in multiple objects and all will be copied into the first object.
function addProperty(object, property) {
return Object.assign(object, { [property]: null });
}
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
February 21, 2026 - The object being inherited from ... chain for more information. You can add a property to all objects created through a certain constructor using the prototype property....
Can't add properties to javascript object
trying to do a very simple operation in some javascript code. trying to add some properties but they are not getting added. I log object before and after and they are not getting added. I have verified that the variable containing the object to be altered is of type “object” and the new ... More on forum.freecodecamp.org
How to add a property to an object that doesn't exist in its type?
In case anyone is still wondering return {...apiResponse, D: 123}; More on reddit.com
The shortest way to conditionally insert properties into an object literal
I don't really like using tricks like this in my code unless I'm the only person who will be reading it. Very few people would understand what the code is doing unless they're already familiar with the trick. I'd rather just add the extra 2-3 lines of code and avoid the risk of confusing people. I'm primarily a JS developer but I write/read a good amount of Perl code at work from time to time. Tricks like this seem like the standard for Perl developers and it can make it very hard to parse through Perl code when you're not already an expert. I try to avoid the same patterns in my JS. More on reddit.com
Understanding how properties are added to objects
It's not "weird" or "funky", it's just how it's done in JS. No "rules". As someone from an another language, this seems error-prone? It certainly is, which is one reason we have TypeScript now. More on reddit.com
Videos
The Best Way to Add Properties to an Object in JavaScript?
12:39
Object Literals | Adding and Updating Properties of Objects | Learn ...
02:55
JavaScript Basics - How to Add and Delete Object Properties - YouTube
00:35
How to add properties to a javascript object dynamically #javascript ...
01:27
How to add property to an object in JavaScript - YouTube
How to Conditionally Add Properties to Objects in JavaScript - YouTube
W3Schools
w3schools.com › js › js_object_properties.asp
JavaScript Object Properties
Object properties are key:value pairs · Access properties with dot notation or bracket notation · Add, change, and delete properties using assignment and delete · Use the in operator to check if a property exists · What are JavaScript Objects? What are Object Methods?
Scaler
scaler.com › home › topics › how to add property to an object in javascript?
How to Add Property to an object in JavaScript? - Scaler Topics
May 4, 2023 - Some of the ways to add property to object JavaScript include: using dot notation, using bracket [ ] notation, using defineProperty() method, using spread operator, and using Object.assign() method.
Open Graph
ogp.me
The Open Graph protocol
In order for your object to be represented within the graph, you need to specify its type. This is done using the og:type property: ... When the community agrees on the schema for a type, it is added to the list of global types.
Leaflet
leafletjs.com › examples › quick-start
Quick Start Guide - Leaflet - a JavaScript library for interactive maps
It allows you to react to user interaction: function onMapClick(e) { alert("You clicked the map at " + e.latlng); } map.on('click', onMapClick); Each object has its own set of events — see documentation for details. The first argument of the listener function is an event object — it contains useful information about the event that happened. For example, map click event object (e in the example above) has latlng property which is a location at which the click occurred.
Reddit
reddit.com › r/typescript › how to add a property to an object that doesn't exist in its type?
r/typescript on Reddit: How to add a property to an object that doesn't exist in its type?
September 28, 2022 -
EDIT: I just realized I could just deep clone the object and alter that one instead.
Heres a simplified example of what I'm trying to do:
const foo = (apiReponse: {A: number, B: number) => {
return apiReponse.D = 123
}Basically I am pass in an api response and using the type thats generated from codegen (apollo graphql). However I want to add a new field to it dynamically, however its throwing an error for the obvious reason.
How do I get around this or this just one big anti pattern?
Top answer 1 of 5
12
In case anyone is still wondering return {...apiResponse, D: 123};
2 of 5
1
Yes and no. Here's a little more explanation on what is going on. A JavaScript object is just a key-value dictionary/map where you can always add and remove values. That's great, but it allows developers to code in a style that is hard to debug. TypeScript to the rescue! Declaring a type makes sure that those objects only have the keys and values with the expected types, which is a lot easier to debug. Quick Answer: Please try to respect the TypeScript type-system. Maybe there's a different way to structure your code so you don't need to add random properties to objects. The real answer: TypeScrip _is_ JavaScript. You can always sneak a new property on an object, and unpack it later, it's totally fine as long as you keep the rest of the code unaware of this. TypeScript allows you to "force" the complier to skip type checks; you are basically telling it: "trust me bro". Doing a shallow-copy with the spread operator is nice, but note this is NOT adding the property to the previously existing object (which is probably for the best), but if you want to do it just like in JavaScript, then go ahead and use JavaScript: const foo = (apiReponse: {A: number, B: number}) => { (apiResponse as any).D = 123; // this property will be hidden and have no type return apiReponse; } To read the sneaked-in property from outside the function, you can also skip the safety of TypeScript: const fooResponse = foo(apiResponse); // TypeScript error: property D not in {A: number, B: number} console.log(fooResponse.D); // This works, but just like in JavaScript, D could be undefined or anything else console.log((fooResponse as any).D); If you do this, be ready to defend your case when your team (or linter) says that you shoudl never use type "any".
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-dynamic-access-to-an-object-property-in-javascript
How to get dynamic access to an object property in JavaScript ? - GeeksforGeeks
August 23, 2025 - Otherwise, you can use the syntax propertyName: variableName to assign the property to a variable with a different name. If the property is not defined in the object, the variable will be assigned the value undefined. Using bracket notation: In JavaScript, you can also use square bracket notation to destructure an object and access its properties.
W3C
w3.org › TR › IndexedDB
Indexed Database API 3.0
If value is greater than or equal to generator’s current number, then set generator’s current number to value + 1. NOTE: A key can be specified both for object stores which use in-line keys, by setting the property on the stored value which the object store’s key path points to, and for object stores which use out-of-line keys, by passing a key argument to the call to store the record.
Coderwall
coderwall.com › p › m-rmpq › dynamically-creating-properties-on-objects-using-javascript
Dynamically creating properties on objects using javascript (Example)
June 26, 2023 - So now that we know how to create properties on javascript objects, let us look at how to create dynamic properties on Javascript ... This will output the value some_value on the console. Defining a dynamic property using Object.defineProperty. The documentation for Object.defineProperty ... // Example of an object property added with defineProperty with a data property descriptor Object.defineProperty(obj, "property3", {value : 'some value', writable : true, enumerable : true, configurable : true}); // 'property3' property exists on object obj and its value is 37
CoreUI
coreui.io › blog › how-to-conditionally-add-a-property-to-an-object-in-javascript
How to Conditionally Add a Property to an Object in JavaScript · CoreUI
January 13, 2025 - JavaScript provides various ways to create objects, such as using object literals, constructors, or factory functions. The most common and concise method is the object literal, which allows you to define a simple object like this: ... However, if you need to conditionally add properties to an object, you’ll need to combine these methods with conditional logic.
Angular
angular.dev › guide › components › inputs
Accepting data with input properties • Angular
In other respects, model inputs work similarly to standard inputs. You can read the value by calling the signal function, including in reactive contexts like computed and effect. See Two-way binding for more details on two-way binding in templates. You can bind a plain JavaScript property to a model input.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › defineProperty
Object.defineProperty() - JavaScript | MDN
July 20, 2025 - In order to ensure these defaults are preserved, you might freeze existing objects in the descriptor object's prototype chain upfront, specify all options explicitly, or create a null-prototype object. ... const obj = {}; // 1. Using a null prototype: no inherited properties const descriptor = Object.create(null); descriptor.value = "static"; // not enumerable, not configurable, not writable as defaults Object.defineProperty(obj, "key", descriptor); // 2.
Rollup
rollupjs.org › configuration-options
Configuration Options | Rollup
The function receives three arguments: the log level, the log object and the default handler. Log objects have, at a minimum, a code and a message property, allowing you to control how different kinds of logs are handled. Other properties are added depending on the type of log.