Objects
In real life, things are described using properties. A person has a name, age, and address. A product has a price, category, and availability.
In JavaScript, an object allows us to store related data together using key–value pairs.
What Is an Object?
An object is a collection of properties. Each property has a name (key) and a value.
Objects help represent real-world entities in code.
Creating an Object
Objects are created using curly braces { }.
let user = {
name: "Alex",
age: 25,
isMember: true
};
This object stores information about a user.
Accessing Object Properties
You can access object values using dot notation.
console.log(user.name);
console.log(user.age);
Dot notation is simple and commonly used.
Bracket Notation
Bracket notation is useful when property names are dynamic or contain special characters.
console.log(user["isMember"]);
Both notations access the same value.
Updating Object Properties
Object properties can be updated or added at any time.
user.age = 26;
user.city = "San Francisco";
Objects are flexible and dynamic.
Real-World Example
Imagine storing product details in an online store:
let product = {
name: "Laptop",
price: 1200,
inStock: true
};
if (product.inStock) {
console.log("Available for purchase");
}
Objects make it easy to work with structured data.
Common Beginner Mistakes
- Forgetting commas between properties
- Using dot notation for dynamic keys
- Confusing objects with arrays
Remember: objects store properties, arrays store ordered lists.
Thumb Rules
- Use objects to group related data
- Use dot notation whenever possible
- Objects can grow as needed
- Keep object structures meaningful
More on Objects Later
This lesson introduced objects at a beginner-friendly level.
More advanced object concepts are covered later.
- Object methods and behavior are explored in Lesson 36 (OOP Basics)
- Object destructuring is covered in Lesson 31 (Destructuring)
What Comes Next?
Now that you know how to store structured data, the next step is working with built-in utilities.
In the next lesson, we will learn about Date and Math objects.