01 / 20

Objects &
Property Access

Learn how to create JavaScript objects and access their properties using dot notation and bracket notation.

JavaScript

What is an Object?

An object is a collection of related data and functionality. Think of it like a real-world container: just as a backpack holds different items, an object holds different properties, each with a name (key) and a value.

student name : "Alice" age : 16 grade : 11 active : true KEY VALUE

Why Objects Matter

Without objects, you would need a separate variable for every piece of related data. Objects let you group related information together into a single, organized structure.

Without Objects
let name = "Alice";
let age = 16;
let grade = 11;
Three separate variables with no clear connection.
With Objects
let student = { ... };

One variable holds all related data together, clearly organized.

Creating an Object Literal

The most common way to create an object is using curly braces { }. This is called an object literal.

let student = { name: "Alice", age: 16, grade: 11, active: true };

Dot Notation

The simplest way to read or set a property is dot notation: write the object name, a dot, then the property name.

// Reading a property console.log(student.name); // "Alice" console.log(student.age); // 16 // Setting a property student.grade = 12; console.log(student.grade); // 12
student . name "Alice" object property result

Bracket Notation

Bracket notation uses square brackets and a string (or variable) to access properties. It is more flexible than dot notation.

// Reading with a string key console.log(student["name"]); // "Alice" // Using a variable as the key let key = "age"; console.log(student[key]); // 16 // Keys with spaces or special chars student["fav color"] = "blue";
student [ "name" ] "Alice" object key (string or variable) result

Dot vs Bracket: When to Use Each

Dot Notation Bracket Notation
Syntax obj.key obj["key"]
Dynamic keys ✕ Not supported ✓ Use a variable
Spaces in keys ✕ Syntax error ✓ Works fine
Readability ✓ Cleaner Slightly verbose
Best for Simple, known keys Dynamic or unusual keys

Modifying Objects

Objects are mutable: you can add new properties, update existing ones, or delete them at any time.

let car = { brand: "Toyota", year: 2020 }; // Add a new property car.color = "red"; // Update an existing property car.year = 2024; // Delete a property delete car.color;
Add
Assign to a key that doesn't exist yet. JS creates it automatically.
Update
Assign to an existing key. The old value is replaced.
Delete
Use the delete operator to remove a property entirely.

Step-by-Step: Working with Objects

1
Declare the object using let or const with curly braces { }.
2
Define key-value pairs inside the braces, separated by commas.
3
Access properties using dot notation for known, simple keys.
4
Use bracket notation when the key is dynamic (stored in a variable) or contains special characters.
5
Modify as needed: add, update, or delete properties to keep the object current.

Example 1: Building a Profile

const profile = { username: "js_ninja", followers: 2500, verified: false }; // Read the username console.log(profile.username); // "js_ninja" // Update followers profile.followers = 2501; // Add a new property profile.bio = "Loves coding!";

Example 2: Dynamic Keys

When the property name is stored in a variable, bracket notation is the only option.

const settings = { theme: "dark", fontSize: 16, language: "en" }; let userChoice = "theme"; // Bracket notation with a variable console.log(settings[userChoice]); // "dark" userChoice = "fontSize"; console.log(settings[userChoice]); // 16

Example 3: Nested Objects

Objects can contain other objects as values. You chain dot or bracket notation to dig deeper.

const school = { name: "Lincoln High", address: { street: "100 Main St", city: "Springfield", zip: "62704" } }; // Access nested property console.log(school.address.city); // "Springfield"
school .address address { } .city "Springfield" school.address.city Chain notation to reach nested values

What Happens with Missing Keys?

If you access a property that does not exist, JavaScript does not throw an error. It simply returns undefined.

const book = { title: "1984", author: "Orwell" }; console.log(book.pages); // undefined console.log(book["isbn"]); // undefined

Objects Can Hold Functions Too

When a function is stored as a property, it is called a method. You call it with dot notation and parentheses.

const calculator = { brand: "MathPro", add: function(a, b) { return a + b; } }; console.log(calculator.add(3, 5)); // 8

Common Mistakes

✕ Quoting the Variable
Writing obj["key"] when you mean obj[key] (no quotes). Quotes make it a literal string, not the variable's value.
✕ Dot with Variables
Writing obj.key when key is a variable. Dot notation always looks for a literal property named "key".
✕ Missing Commas
Forgetting the comma between properties causes a syntax error. Every property except the last needs a trailing comma.

Summary Cheat Sheet

Action Syntax Example
Create object { key: value } { name: "Alice" }
Read (dot) obj.key student.name
Read (bracket) obj["key"] student["name"]
Add / Update obj.key = val student.grade = 12
Delete delete obj.key delete student.age
Dynamic key obj[variable] settings[userChoice]

Question 1 of 5

Given the object below, what does pet.species return?

const pet = { species: "cat", age: 3 };

Question 2 of 5

You have let key = "color"; and let shirt = { color: "blue", size: "M" };. Which expression returns "blue"?

Question 3 of 5

What is logged after running this code?

const user = { name: "Bob" }; user.age = 25; console.log(user.age);

Question 4 of 5

Which notation must you use to access a property whose key contains a space, like "first name"?

Question 5 of 5

What does team.coach.name return?

const team = { name: "Wolves", coach: { name: "Rivera", wins: 42 } };