add new property to object javascript es6 code example
Example 1: how to add property to object in javascript
var data = {
'PropertyA': 1,
'PropertyB': 2,
'PropertyC': 3
};
data["PropertyD"] = 4;
alert(data.PropertyD);
alert(data["PropertyD"]);
Example 2: add property to object javascript
let yourObject = {};
let yourKeyVariable = "yourKey";
yourObject[yourKeyVariable] = "yourValue";
yourObject["yourKey"] = "yourValue";
yourObject.yourKey = "yourValue";
Example 3: add new properties to JS objects
person.nationality = "English";