Javascript — Objects and their internal representation

Objects in Javascript are used to store a keyed collection of various data and other complex entities. They are an important part of the Javascript system.

Objects are denoted using the curly braces or the flower {} brackets with an optional list of properties. A property is a ‘key-value’ pair where the key is a string and value can be anything.

We can create an empty Object using either of the two following methods:

let user = new Object(); // object constructor syntax

let user = {}; // object literal syntax

We can immediately enter some properties into the object that we have created.

let user = { //an object

name : “ABC”, //a key called ‘name’ and a value of ‘ABC’

age : 30 // a key called ‘age’ and a value of 30

};

Now, in the user object, there are two properties:

  1. A property with a key called ‘name’ and a value of ‘ABC’
  2. A property with a key called ‘age’ and a value of 30

From this object, we can add, remove and change the properties at any time we want. Property values can be accessed using the dot (.) notation.

console.log(user.name) //ABC

console.log(user.age) // 30

We can add a property to the object.

user.isAdmin = true;

Now the user object is updated and the “isAdmin : true” property is added to the object.

We can also have multiword key names but they need to be quoted.

let catGuy = {

“likes dogs” = true;

};

We can delete a property using the ‘delete’ operation.

delete user.age;

This will remove the age property from the object user.