add new key and value to object javascript code example
Example 1: js add key to object
const obj = {
key1: value1,
key2: value2
};
obj["key3"] = value3;
obj.key4 = value4;
Example 2: javascript add to object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Example 3: add new key value pair to object javascript
const person = {
name: "Elon Musk"
}
person.age = 49;
console.log(person);