Objects &
Property Access
Learn how to create JavaScript objects and access their properties using dot notation and bracket notation.
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.
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.
let name = "Alice";let age = 16;let grade = 11;Three separate variables with no clear connection.
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
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";
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;
delete operator to remove a property entirely.Step-by-Step: Working with Objects
let or const with curly braces { }.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"
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
obj["key"] when you mean obj[key] (no quotes). Quotes make it a literal string, not the variable's value.obj.key when key is a variable. Dot notation always looks for a literal property named "key".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 };
pet.species returns the value associated with the key "species", which is "cat".Question 2 of 5
You have let key = "color"; and let shirt = { color: "blue", size: "M" };. Which expression returns "blue"?
shirt[key] evaluates the variable key to "color", then accesses shirt["color"] which is "blue". Option A looks for a literal property named "key". Option B references an undefined variable color.Question 3 of 5
What is logged after running this code?
const user = { name: "Bob" };
user.age = 25;
console.log(user.age);
const prevents reassigning the variable (user = {} would fail), but you can still add or modify properties on the object. So user.age = 25 works and logs 25.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
}
};
team.coach returns the nested object { name: "Rivera", wins: 42 }. Then .name on that nested object returns "Rivera". The outer team.name ("Wolves") is a different property.