For reference values, we can add, change, or delete properties and methods at any time.
Consider this example:
let person = new Object(); person.name = "HTML"; console.log(person.name); // "HTML"
Here, an object is created and stored in the variable person.
Next, a property called name is added and assigned the string value of "HTML".
The new property is accessible from that point on, until the object is destroyed or the property is explicitly removed.
Primitive values can't have properties added to them:
let name = "HTML"; name.age = 27;/*from w w w .jav a2 s .c om*/ console.log(name.age); // undefined
Here, a property called age is defined on the string name and assigned a value of 27.
On the next line, the property is gone.
The instantiation of a primitive type can be accomplished using only the primitive literal form.
If you were to use the new keyword, JavaScript will create an Object type.
Here's an example to distinguish between the two:
let name1 = "HTML"; let name2 = new String("CSS"); name1.age = 27;//from ww w . j a va 2 s .c o m name2.age = 26; console.log(name1.age); // undefined console.log(name2.age); // 26 console.log(typeof name1); // string console.log(typeof name2); // object