๐
๐จ
Objects are like ID cards - they hold related info together!
What are Objects?
An object stores related data as key-value pairs. Think of it like a form with labels and values - Name: Alice, Age: 25.
Objects help you organize data that belongs together, like all the info about a person or a product.
๐ก
๐จ
Creating Objects
Use curly braces { } to create objects:
1const person = {2 name: "Alice",3 age: 25,4 city: "New York"5};67// Keys (properties): name, age, city8// Values: "Alice", 25, "New York"
๐ค
๐จ
Accessing Properties
Two ways to get values from an object:
1const person = {2 name: "Alice",3 age: 254};56// Dot notation (most common)7console.log(person.name); // "Alice"8console.log(person.age); // 25910// Bracket notation (for dynamic keys)11console.log(person["name"]); // "Alice"1213const key = "age";14console.log(person[key]); // 25
๐ก
๐จ
Modifying Objects
1const person = {2 name: "Alice",3 age: 254};56// Change a value7person.age = 26;89// Add new property10person.job = "Developer";1112// Delete a property13delete person.age;1415console.log(person);16// { name: "Alice", job: "Developer" }
๐ค
๐จ
Nested Objects & Arrays
1const user = {2 name: "Bob",3 address: {4 street: "123 Main St",5 city: "Boston"6 },7 hobbies: ["reading", "gaming"]8};910// Access nested data11console.log(user.address.city); // "Boston"12console.log(user.hobbies[0]); // "reading"1314// Add to nested array15user.hobbies.push("coding");
๐ฎ
Try It Yourself!
Edit the person object below. Change properties and see the object update!
const person = {
name:
age:
city:
hobbies:
"coding""reading"
}
1const person = {2 name: "Alice",3 age: 25,4 city: "New York",5 hobbies: ["coding", "reading"]6};78// Access values9person.name; // "Alice"10person.age; // 2511person.hobbies[0]; // "coding"
Quick Access
person.name"Alice"
person.age25
person.city"New York"
person.hobbies.length2
๐งช Code Lab - Try It Yourself!
๐งชObjects Lab
Enter RunShift+Enter New line History
๐Output
Run some code to see the output here!
๐กTry These Examples