Object and it’s internal representation — JavaScript

Abhijeet Pawar
2 min readNov 2, 2020

--

What is a JavaScript Object

Objects in JavaScript consist of key/value pairs of any type, and they are similar to a Dictionaries in Python, HashMaps in Java, etc. Let’s take a look!

Creating JavaScript Objects

Use { } to create an empty object.

const person = {};

You can also define an object with key/value pairs already included by using the object literal syntax. In this case, I’m creating an object with four properties: first, last, age, and email.

const person = {
first: "Abhijeet",
last: "Pawar",
age: 23,
email: "js@learn.com"
}

Working with Object properties

After creating an object with properties, you can access each property by using the dot notation. After your object you use a dot (.) followed by the name of the property you are looking for.

console.log(person.first) //Abhijeet
console.log(person.last) //Pawar
console.log(person.age) //23

Alternatively, you can use the following syntax by using brackets and the name of the property you are looking for inside of the brackets.

console.log(person["first"]) //Abhijeet
console.log(person["last"]) //Pawar
console.log(person["age"]) //23

You can also used dot notation or named notation to update properties as well.

person.name = abhi;
person.age = 24;
console.log(person.name) //abhi
console.log(person.age) //24

To delete a property from an object, you can call delete followed by the property you want to remove. After removing a property, it will return undefined when you try to access it.

delete person.age
console.log(person.age); //undefined

Comparing objects

In JavaScript, objects are a reference type. Two distinct objects are never equal, even if they have the same properties. Only comparing the same object reference with itself yields true.

// Two variables, two distinct objects with the same properties
let p1 = { name:"alex" };
let p2 = { name:"john" };
//Here p1 and p2 are pointing to two different objects
p1==p2; //false
p1===p2; //false
//Two variables, single object
let p1 = { name:"sam" };
let p2 = p1;
//Here p1 and p2 are pointing to same object
p1==p2; //true
p1===p2; //true

--

--