On this page

JavaScript Objects

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

In JavaScript, an object is a complex data type used to store collections of data and more complex entities. While primitive data types like strings or numbers represent a single value, an object allows you to group related data together in a structured format using key-value pairs. This makes objects essential for representing real-world items, such as a user profile, a product in a store, or a configuration setting.

Object Structure and Key-Value Pairs​

An object is defined using curly braces {}. Inside these braces, data is organized into pairs. Each pair consists of a key (also known as a property name) and a value.


const car = {

  make: "Toyota",

  model: "Camry",

  year: 2024,

  isElectric: false,

};

In this example, make is a key, and "Toyota" is its corresponding value. Each pair is separated by a comma.

Accessing Properties​

Once an object is created, you need a way to retrieve its data. JavaScript provides two primary methods for property access: Dot Notation and Bracket Notation.

Dot Notation​

Dot notation is the most common and readable way to access object properties. You append a dot (.) followed by the property name to the object variable.


console.log(car.make); // Output: Toyota

console.log(car.year); // Output: 2024

Bracket Notation​

Bracket notation involves placing the property name as a string inside square brackets []. This method is more flexible than dot notation.


console.log(car["model"]); // Output: Camry

When to Use Bracket Notation Over Dot Notation​

While dot notation is preferred for its simplicity, bracket notation is required in several specific professional scenarios.

Using Dynamic Keys (Variables)​

If the name of the property you want to access is stored in a variable, you must use bracket notation. Dot notation will look for a literal property with the name of that variable.


const propertyToRead = "make";



console.log(car.propertyToRead); // undefined (looks for a key named 'propertyToRead')

console.log(car[propertyToRead]); // Output: Toyota (evaluates the variable)

Accessing Non-Standard Keys​

If a property key contains spaces, hyphens, or starts with a number, dot notation will result in a syntax error. Bracket notation handles these strings easily.


const user = {

  "first name": "John",

  "account-type": "Premium",

};



// console.log(user.first name);   // Syntax Error

console.log(user["first name"]); // Output: John

Modifying and Adding Properties​

Objects in JavaScript are mutable, meaning you can change their contents after they are created. You can use either notation to update existing values or create entirely new properties.


const laptop = {

  brand: "Dell",

  ram: 8,

};



// Updating an existing property

laptop.ram = 16;



// Adding a new property

laptop.color = "Silver";



console.log(laptop); // { brand: "Dell", ram: 16, color: "Silver" }

Practical Implementation Principles​

To ensure your code remains maintainable and efficient, follow these industry standards:

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Objects and Classes

Next

this Keyword