Objects
Prerequisites: Arrays
An object stores key/value pairs — instead of numbered positions like an array, each value has a named key.
Syntax
const user = { name: "Ada", age: 25 };
Example
console.log(user.name);
user.age = 26;
for (const key in user) {
console.log(key, user[key]);
}
Expected output
Ada
name Ada
age 26
Use object.property (dot notation) when the key name is known ahead
of time, or object["property"] (bracket notation) when it's stored in
a variable.
Practice
Create an object representing a book with title and author
properties, then log `${title} by ${author}`.
Show solution
const book = { title: "Dune", author: "Frank Herbert" };
console.log(`${book.title} by ${book.author}`);