Objects · Lesson 23 of 45
Object basics
Group information about one thing in one place.
8 minutes3 games
An object groups properties inside { }, each with a name (key) and a value. Read one with a dot, user.name, or brackets, user["name"], when the name is in a variable or has a space.
- student
- name: "Youssef"
- level: 3
- skills
- 0: "HTML"
- 1: "JS"
Objects can hold arrays and other objects.
JavaScript
const student = {
name: "Youssef",
level: 3,
skills: ["HTML", "JS"],
"favorite color": "blue",
}
student.level = 4 // change
student.city = "Cairo" // add
delete student.skills // remove
const key = "name"
console.log(student[key], student.level, student["favorite color"])
console.log("city" in student)Looping over an object
JavaScript
const prices = { tea: 10, coffee: 25, juice: 18 }
console.log(Object.keys(prices))
console.log(Object.values(prices))
for (const [drink, price] of Object.entries(prices)) {
console.log(`${drink}: ${price} EGP`)
}Quick question
How do you read the page count in const book = { pages: 300 }?
Spot the bug
One line prints undefined instead of the price. Which?
Exercise
Print the sum of every price in the object (53).
Expected output: 53
JavaScript
const prices = { tea: 10, coffee: 25, juice: 18 }
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.