Assigning Object Properties

Overview

Statically Assigning Object Properties
Object properties can be assigned when the object is first declared.

let obj = { name:'test', size:20 };




If a property name contains a space, a hypen or other whitespace or punctuation, quotes need to be placed around the property name.

let obj = { name:'test', "object size":20 };
Dynamically Assigning Object Properties
Properties on an object can change. They can be assigned after the object has been created. The following example creates an empty object, and then assigns the value of "size" to the object.

let obj = {}; obj['size'] = 10;


let obj = {}; obj.size = 10;
Deleting Object Properties
Properties on an object can be removed using the delete keyword.

let obj = { size:20 }; delete obj['size']; //alternatively delete obj.size;
Spread Operator
The spread operator (...) can be used to quickly copy properties from one object to another.

let obj = { size:20, name:'test' }; let obj2 = {...obj}