Hello! Welcome back to your JavaScript journey.
In our last lesson, you learned how to create objects using object literal syntax, bundling related data and functionality into a single, organized unit. We saw that creating objects is the first step, but the real power comes from being able to interact with them.
Today, we'll build directly on that foundation. This lesson addresses the learning outcome: Access, add, and modify object properties using dot and bracket notation. You'll learn the two essential syntaxes for reading data from an object, updating existing data, and adding new information on the fly. Mastering these operations is fundamental to manipulating data in any JavaScript application you build.
Two Paths to the Same Data: Dot and Bracket Notation
Once you have an object, there are two primary ways to get, change, or add its properties: dot notation and bracket notation. For many simple cases, they are interchangeable, but they have a crucial difference that makes each one uniquely powerful.
Let's start with a foundational reading that introduces both syntaxes.
JavaScript Objects - A Detailed Guide
The article 'JavaScript Objects - A Detailed Guide' from Launch School provides a concise introduction to both notations. It will show you the basic syntax for accessing and adding properties.
Read the first part of the 'What are Objects?' section. Start from the top and stop just before the example about the 'delete' keyword. Focus on the syntax for dot notation (person.name) and bracket notation (person['age']).
As you just read, the syntax is quite direct:
- Dot Notation:
objectName.key - Bracket Notation:
objectName['key']
Here is a visual breakdown of these two approaches:
Accessing, Modifying, and Adding Properties
The great thing about these notations is their consistency. The same syntax you use to read a property is also used to change or add one.
Let's take a simple user object:
let user = {
name: 'Alex',
age: 30
};
-
Accessing (Reading) a Property:
- Dot:
console.log(user.name);// Outputs: 'Alex' - Bracket:
console.log(user['age']);// Outputs: 30
- Dot:
-
Modifying (Updating) a Property: You just use the assignment operator (
=).- Dot:
user.age = 31; - Bracket:
user['name'] = 'Alexandra'; - Now
useris{ name: 'Alexandra', age: 31 }.
- Dot:
-
Adding a New Property: The syntax is identical to modifying. If the property doesn't exist, JavaScript creates it.
- Dot:
user.isOnline = true; - Bracket:
user['city'] = 'Codeville'; - Now
useris{ name: 'Alexandra', age: 31, isOnline: true, city: 'Codeville' }.
- Dot:
This visual shows these actions in a code editor, along with some helpful notes.

The Critical Difference: Static vs. Dynamic Keys
So if both notations can do the same things, why have two? This gets to the "deep theory" part you enjoy. The answer lies in how they handle property keys.
- Dot notation requires a static key. This means the exact name of the key must be known and typed out directly in the code. It must also be a valid JavaScript identifier (e.g., no spaces, not starting with a number).
- Bracket notation allows for dynamic keys. The key inside the brackets is an expression that gets evaluated. The result of that evaluation is what JavaScript uses to look up the property. This is incredibly powerful.
This video from the channel Deeecode provides an excellent visual explanation of this core difference.
DOT NOTATION vs BRACKET NOTATION in JavaScript
This video, 'DOT NOTATION vs BRACKET NOTATION in JavaScript', clearly demonstrates the syntax for both methods and then dives into the crucial distinction between static and dynamic keys.
Please watch from 00:31 to 08:49. The first two parts (until 03:44) will reinforce the syntax for accessing, modifying, and adding properties. The main part to focus on is from 03:44 onwards, where the video explains why dot notation fails with variables and how bracket notation solves this problem.
As the video demonstrated, you must use bracket notation in two main scenarios:
-
When the key is stored in a variable. This is common when iterating through object keys or when a function needs to access a property based on one of its arguments.
let keyToAccess = 'name'; console.log(user.keyToAccess); // UNDEFINED! Tries to find a key literally named "keyToAccess". console.log(user[keyToAccess]); // 'Alexandra'. Correct! Evaluates the variable to 'name'. -
When the key is not a valid identifier. If a key has spaces, special characters, or starts with a number, you cannot use dot notation.
let userPreferences = { 'theme color': 'dark', 'font-size': 16 }; // console.log(userPreferences.theme color); // Syntax Error! console.log(userPreferences['theme color']); // 'dark'. Correct!
Rule of thumb: Use dot notation by default because it's cleaner and easier to read. Switch to bracket notation only when you need its dynamic capabilities or when the key is an invalid identifier.
Test your understanding!
Consider the following code:
let settings = {
theme: 'light',
fontSize: 14,
'show-avatars': true
};
function updateSetting(settingName, value) {
// Your code here. How do you update the 'settings' object
// using the 'settingName' variable?
}
// How would you call this function to change the 'show-avatars' setting to false?
Show answer
Inside the function, you must use bracket notation because settingName is a variable.
function updateSetting(settingName, value) {
settings[settingName] = value;
}
// To call it:
updateSetting('show-avatars', false);
// After the call, the settings object will be:
// {
// theme: 'light',
// fontSize: 14,
// 'show-avatars': false
// }
You couldn't use settings.settingName = value; because that would try to create a new property literally named settingName.
Guided Practice: Manipulating Objects
Now let's apply these concepts in a series of practical exercises. The following video walks through several challenges that involve creating and manipulating objects. We'll go through it step-by-step.
Exercises: Objects - Javascript In Depth
The video 'Exercises: Objects' from the channel Tech with Nader provides a fantastic set of guided problems. We'll work through them to solidify your understanding.
This is a long video with multiple exercises. I will guide you through each one. You can pause the video before he reveals the solution to try it yourself!
Exercise 1: Basic Manipulation
This first exercise involves changing an ID and adding an item to an array within an object. It's a great warm-up.
Watch: From 00:47 to 07:10.
Focus on:
- How he uses both dot and bracket notation to change the
id. - The logic of accessing the
neighborsarray (mexico.neighbors) before using an array method (.push()) on it.
Exercise 2: Adding and Removing Properties
This exercise reinforces adding new properties and demonstrates how to remove items from an array property.
Watch: From 07:45 to 15:13.
Focus on:
- How adding a new property (
color) uses the same syntax as modifying an existing one (breed). - The use of
myPet.friends.pop()to remove the last item from thefriendsarray.
Exercise 3: Working with Nested Objects and References
This exercise is more complex and touches on a core concept you asked about: how things work "under the hood." It deals with objects that contain other objects and arrays of objects.
Watch: From 15:52 to 30:48.
Focus on:
- Chaining: How to access nested properties, like
store.products[0].price. You chain dot and bracket notation to dig deeper into the structure. - Objects as References: Pay close attention around 27:10. The instructor shows that when he changes the banana's price through the store object, the original
bananaobject also changes. This is because the array contains a reference to the original object, not a copy. This is a critical concept we will revisit.
Exercise 4: Deleting Properties and Invalid Keys
This final exercise introduces a new keyword and reinforces the main reason for bracket notation.
Watch: From 30:48 to 40:45.
Focus on:
- The
deletekeyword used to completely remove a key-value pair (delete houseForSale.built). - The
sale pricekey contains a space. Notice how he must use bracket notation (houseForSale['sale price'] = ...) to add this property. Dot notation would cause a syntax error.
Conclusion
Excellent work today! You have now learned the essential mechanics of interacting with JavaScript objects. This is a skill you will use constantly as you build more complex programs. By understanding not just how to use dot and bracket notation but why and when to use each, you've gained a much deeper understanding that will help you write more robust and bug-free code.
Key Takeaways:
- You can access, modify, and add object properties using dot (
.) and bracket ([]) notation. - Dot notation is for static keys that are known ahead of time and are valid identifiers.
- Bracket notation is for dynamic keys (e.g., from a variable) or keys that contain spaces or special characters.
- You can chain notations to access properties in nested objects and arrays (e.g.,
data.users[0].name). - The
deletekeyword can be used to completely remove a property from an object.
Next Up:
In our last lesson, we briefly saw that functions can be properties on an object. We called them "methods." In the next lesson, we will focus entirely on them. You will learn how to define methods on objects to represent behavior and explore the crucial this keyword, which allows a method to access its own object's data.