3.1 What is an Object?
An object is a collection of named properties. Each property has a key (property name) and a value.
let person = {
name: "Alice",
age: 25,
isStudent: true
};
| Key | Value |
|---|---|
| name | "Alice" |
| age | 25 |
| isStudent | true |
3.2 Object Literal Syntax
You create an object using curly braces { }. Inside them, properties are written as key : value pairs separated by commas.
let person = {
name: "Alice", // line 2
age: 25, // line 3
isStudent: true // line 4
};
console.log(person.name); // line 7
console.log(person.age); // line 8
How the example works
- let person = { starts an object literal assigned to person.
- name: "Alice" creates a property named name with the value "Alice".
- age: 25 creates an age property with a numeric value.
- isStudent: true creates an isStudent property with a Boolean value.
- } ends the object literal.
- person.name uses dot notation to access the name property.
- person.age uses dot notation to access the age property.
3.3 Accessing and Modifying Properties
JavaScript provides two common ways to access object properties:
| Method | Syntax | Example |
|---|---|---|
| Dot notation | object.propertyName | car.brand |
| Bracket notation | object["propertyName"] | car["model"] |
You can also add new properties or change existing properties after the object has been created.
let car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car.brand); // line 5
console.log(car["model"]); // line 6
car.year = 2020; // line 8
car["color"] = "blue"; // line 9
console.log(car.year); // line 11
console.log(car.color); // line 12
What happens here?
car.brandaccesses brand using dot notation.car["model"]accesses model using bracket notation.car.year = 2020adds a new year property.car["color"] = "blue"adds a new color property.
3.4 Objects Containing Multiple Types
Object property values can be different JavaScript data types. The source material highlights strings, numbers, booleans, arrays, other objects, and functions (methods).
let book = {
title: "JavaScript Guide",
pages: 300,
available: true
};
console.log(book.title); // line 6
console.log(book.pages); // line 7
console.log(book.available); // line 8
Here, title stores a string, pages stores a number, and available stores a Boolean value.
4. Arrays of Objects and Objects of Arrays
Real-world data often requires combining arrays and objects. This lets us represent collections of similar records or group related lists under named keys.
4.1 Array of Objects
An array of objects is useful when you have a list of similar entities, each with named fields.
let students = [
{ name: "Alice", age: 20 }, // line 2
{ name: "Bob", age: 22 }, // line 3
{ name: "Cara", age: 21 } // line 4
];
console.log(students[0].name); // line 7
console.log(students[1].age); // line 8
Understanding the access pattern
studentsis an array.- Each
{ ... }is an object representing one student. students[0].namegets the first object and then reads itsname.students[1].agegets the second object and then reads itsage.
let students = [
{ name: "Alice", age: 20 },
{ name: "Bob", age: 22 },
{ name: "Cara", age: 21 }
];
for (let i = 0; i < students.length-1; i++) {
console.log(students[i].name + " is " + students[i].age);
}
The loop accesses each student object through students[i], then reads its name and age properties.
for...of with for...inlet students = [
{name:'bob', branch:"CSE-AIML", fee:350000},
{name:'ben', branch:"CSE", fee:300000},
{name:'bint', branch:"CSE-DS", fee:300000}
];
for(let value of students){
for(let key in value){
console.log(key+" is "+value[key]);
}
console.log("----------------");
}
for...of gets each student object from the array. Inside it, for...in visits the keys of that object, and value[key] reads the corresponding value.
4.2 Object of Arrays
An object of arrays is useful when you group several related lists under named keys.
let course = {
titles: ["Math", "Physics", "Chemistry"], // line 2
credits: [3, 4, 3] // line 3
};
console.log(course.titles[1]); // line 6
console.log(course.credits[1]); // line 7
How it works
courseis an object with two properties.titlesis an array containing course names.creditsis an array containing credit values.course.titles[1]accesses "Physics".course.credits[1]accesses 4, the credits for Physics.
Quick Review
| Concept | What it represents | Example access |
|---|---|---|
| Object | Named properties | person.name |
| Array of objects | A list of similar records | students[0].name |
| Object of arrays | Named groups of related lists | course.titles[1] |
students[0].name, read it from left to right: first select the array element, then select the object's property.