Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

Updated
•4 min read•View as Markdown

What objects are and why they are needed ?

Objects in JavaScript are data structures used to store related information together in the form of key–value pairs. Each key represents a property name and each value stores the data related to that property. Objects help represent real-world entities such as a student, car, or user with their details in one place. They are needed because they make code more organized, readable, and easier to manage. Instead of creating many separate variables, we can group related data inside a single object. Objects can also store functions along with data, which helps developers build structured and more complex applications.

Creating objects

Creating objects in JavaScript means making a new object to store related data using key–value pairs. There are different ways to create objects.

The most common way is using object literals. In this method, we create an object using curly braces {} and add properties inside it.

Example:

let student = { name: "Divit", age: 13, class: "7" };

Another way is using the new Object() constructor.

Example:

let car = new Object();

car.brand = "Toyota";

car.color = "Red";

Accessing properties (dot notation and bracket notation)

Accessing properties in JavaScript means getting the values stored inside an object. There are two main ways to do this: dot notation and bracket notation.

Dot notation is the most common and simplest way to access a property. In this method, we write the object name followed by a dot and the property name. It works when the property name is known and written normally.

Example:

let player = { name: "Divit", score: 150 };

console.log(player.name);

Bracket notation accesses properties using square brackets with the property name written as a string. It is useful when property names contain spaces or when the property name is stored in a variable.

Example:

let game = { "player level": 5 }; console.log(game["player level"]);

Updating object properties

Updating object properties in JavaScript means changing the value of an existing property inside an object. Objects store data as key–value pairs, and sometimes the stored value needs to be modified when the data changes. This can be done by accessing the property and assigning a new value to it. Updating properties is useful when information like scores, user details, or settings change during a program. JavaScript allows developers to easily update values without creating a new object. This makes objects flexible and useful for dynamic data.

Example:

let player = { name: "Divit", score: 100 }; player.score = 200; console.log(player.score);

Adding and deleting properties

Adding and deleting properties in JavaScript objects allows developers to change the structure of an object when needed. A new property can be added by simply writing the object name, followed by a dot and the new property name, then assigning it a value. This is useful when new information needs to be stored later in the program. Properties can also be removed from an object using the delete keyword. This helps remove data that is no longer required. These features make JavaScript objects flexible and easy to modify during program execution.

Example:

let player = { name: "Divit", score: 100 }; player.level = 5; // adding property
delete player.score; // deleting property

console.log(player);

Looping through object keys

Looping through object keys in JavaScript means going through each property of an object one by one. This is useful when we want to read or use all the values stored in an object without accessing each property manually. The most common way to loop through object keys is by using the for...in loop. This loop goes through every key in the object and allows us to access its value. It helps when working with objects that have many properties or when the number of properties is unknown.

Example:

let player = { name: "Divit", score: 100, level: 5 }; for (let key in player) { console.log(key, player[key]); }