Object Basics
Objects are one of the most important data structures in JavaScript.
You have already worked with arrays, which store ordered lists:
const user = ["Alice", 25, "admin"];This works, but it is not very clear.
What does index 0 mean?
What does index 1 mean?
Objects solve this problem by storing data with names.
const user = {
name: "Alice",
age: 25,
role: "admin",
};Now the meaning is obvious.
Objects store data as key-value pairs.
What Is an Object?
An object is a collection of related data.
Each piece of data is stored as a property.
const product = {
name: "Keyboard",
price: 50,
inStock: true,
};This object has three properties:
namepriceinStock
Each property has a value:
| Key | Value |
|---|---|
name |
"Keyboard" |
price |
50 |
inStock |
true |
Objects are useful when values belong together and each value has a clear name.
Creating an Object
The most common way to create an object is with an object literal.
const user = {
name: "Ava",
email: "ava@example.com",
isActive: true,
};Object literals use curly braces:
{}Inside the braces, properties are written as:
key: valueMultiple properties are separated with commas.
const course = {
title: "JavaScript Fundamentals",
level: "beginner",
lessons: 12,
};Property Values Can Be Any Type
Object property values can be any JavaScript value.
const profile = {
name: "Mira",
age: 28,
isVerified: true,
favoriteTopics: ["arrays", "objects", "functions"],
address: {
city: "Delhi",
country: "India",
},
};This object contains:
- strings
- numbers
- booleans
- an array
- another object
Objects can model real-world data because real data is often mixed and nested.
Accessing Properties With Dot Notation
Use dot notation when you know the property name.
const user = {
name: "Alice",
role: "admin",
};
console.log(user.name); // Alice
console.log(user.role); // adminDot notation is the most common way to access object properties.
It is simple and readable.
object.propertyAccessing Properties With Bracket Notation
You can also use bracket notation.
const user = {
name: "Alice",
role: "admin",
};
console.log(user["name"]); // Alice
console.log(user["role"]); // adminBracket notation uses a string property name:
object["property"]This is useful when the property name is stored in a variable.
const user = {
name: "Alice",
role: "admin",
};
const key = "role";
console.log(user[key]); // adminIf you wrote user.key, JavaScript would look for a property literally named key.
console.log(user.key); // undefinedWhen Bracket Notation Is Required
Bracket notation is required when a property name is not a valid identifier.
const settings = {
"dark-mode": true,
"font size": 16,
};
console.log(settings["dark-mode"]); // true
console.log(settings["font size"]); // 16You cannot write:
settings.dark-mode;JavaScript would treat - as subtraction.
For normal property names, use dot notation.
For dynamic or unusual property names, use bracket notation.
Adding Properties
You can add properties after an object is created.
const user = {
name: "Alice",
};
user.role = "admin";
user.isActive = true;
console.log(user);Result:
{
name: "Alice",
role: "admin",
isActive: true
}You can also add properties with bracket notation:
user["lastLogin"] = "2026-06-14";Updating Properties
To update a property, assign a new value.
const product = {
name: "Mouse",
price: 25,
};
product.price = 30;
console.log(product.price); // 30Objects are mutable.
That means you can change their contents.
Deleting Properties
You can remove a property with the delete operator.
const user = {
name: "Alice",
password: "secret",
};
delete user.password;
console.log(user); // { name: "Alice" }Use delete carefully.
In many applications, it is clearer to create a new object without the property or set the value to null when you intentionally want to show absence.
Missing Properties
If you access a property that does not exist, JavaScript returns undefined.
const user = {
name: "Alice",
};
console.log(user.email); // undefinedThis does not throw an error.
It simply means the property is missing.
Nested Objects
Objects can contain other objects.
const user = {
name: "Alice",
address: {
city: "Mumbai",
country: "India",
},
};
console.log(user.address.city); // MumbaiYou can chain property access:
user.address.cityRead it like this:
Get user.
Then get address.
Then get city.Safe Nested Access
If a nested object might be missing, direct access can fail.
const user = {
name: "Alice",
};
console.log(user.address.city); // TypeErroruser.address is undefined.
Trying to read .city from undefined causes an error.
Use optional chaining for safer access:
console.log(user.address?.city); // undefinedYou can combine it with nullish coalescing:
const city = user.address?.city ?? "Unknown";
console.log(city); // UnknownObjects vs Arrays
Use arrays when order matters and items are similar.
const scores = [90, 85, 72];Use objects when values have names.
const user = {
name: "Alice",
age: 25,
role: "admin",
};Often, you will combine both:
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" },
];This is one of the most common data shapes in JavaScript: an array of objects.
Best Practices
Use clear property names:
const user = {
firstName: "Ava",
isActive: true,
};Prefer dot notation for normal properties:
user.firstName;Use bracket notation for dynamic keys:
const key = "firstName";
user[key];Group related data together:
const product = {
name: "Keyboard",
price: 50,
inStock: true,
};Use optional chaining when nested data may be missing:
const city = user.address?.city;Common Mistakes
Mistake 1: Using an Array When an Object Is Clearer
const user = ["Alice", 25, "admin"];This works, but the meaning depends on remembering indexes.
Better:
const user = {
name: "Alice",
age: 25,
role: "admin",
};Mistake 2: Confusing Dot and Bracket Notation
const user = {
name: "Alice",
};
const key = "name";
console.log(user.key); // undefined
console.log(user[key]); // AliceUse bracket notation when the property name is stored in a variable.
Mistake 3: Accessing Nested Properties Unsafely
const user = {
name: "Alice",
};
console.log(user.address.city); // TypeErrorUse optional chaining:
console.log(user.address?.city); // undefinedQuick Check
What does this log?
const book = {
title: "JavaScript Basics",
pages: 250,
};
console.log(book.title);It logs:
JavaScript BasicsWhat does this log?
const user = {
name: "Mira",
};
const key = "name";
console.log(user[key]);It logs:
MiraSummary
Objects store related data as key-value pairs.
- Use object literals
{}to create objects. - Object properties have keys and values.
- Use dot notation for normal property access.
- Use bracket notation for dynamic or unusual property names.
- Objects can contain arrays and other objects.
- Missing properties return
undefined. - Use optional chaining for safe nested access.
- Use arrays for ordered lists and objects for named data.