Dynamic Object Properties
Video
JavaScript Notes
JavaScript
/***
* Dynamic Object properties using square brackets
*/
let beverage = 'Beer';
const myObj = {
a: 1,
b: 2,
c: 3,
food: 'cheese',
propName: 'cheese',
beverage: 'Heineken', // beverage: 'Heineken'
[beverage]: 'corona', // Beer: 'corona'
};
let addProp = (obj, propName, propValue) => {
// obj.food = 'cheese';
obj[propName] = propValue;
obj.propName = propValue;
};
addProp(myObj, 'food', 'cheese');
Feedback
Submit and view feedback