A story has a title, an author and a number of views. Instead of three separate variables, you can keep them together in one object, the way a card keeps a person's details together.
flag
What you'll learn
How to keep many related values together in one object.
How to read, change and add properties.
How to show an object on the page, and how to keep a list of objects.
waving_handWelcome to objects
An object groups related values under one name. Each value has its own name too, called a key.
A key and its value together make a property.
You've been using objects since JS Lesson 1: document is an object, and innerHTML is one of its elements' properties.
Even console is an object, and log is a property that holds a function.
badge
Think of a school ID card. One card holds many facts about one student, and each fact has a label: Name, Grade, Section.
In JavaScript, the card is the object, each label is a key, and what's written next to it is the value.
badge
Rizal High School ID
const student = {
Name:Ana Santosname: "Ana Santos",
Grade:7grade: 7,
Section:Sampaguitasection: "Sampaguita",
Enrolled:YesisEnrolled: true
};
Now let's see the object ↓
badge
Rizal High School ID
const student = {
Name:Ana Santosname: "Ana Santos",
Grade:7grade: 7,
Section:Sampaguitasection: "Sampaguita",
Enrolled:YesisEnrolled: true
};
Each line of the card becomes one property: a key, a colon, then the value. Commas go between the properties.
searchA closer look
How an object is written
const story =name
{start
titlekey
:colon
"New phones arrive"value
,comma
views: 120next property
}end
Inside an object, a colon : joins a key to its value. That's different from =, which you use for variables. Keys follow the same rules as variable names.
Two ways to read a property
Dot notation, like story.title, is the short, usual way. Bracket notation, like story["title"], does the same thing with the key in quotes.
Brackets are useful when the key is stored in a variable, like story[field].
Changing a const object
In JS Lesson 2, a const couldn't get a new value. An object made with const still can't be swapped for a different object,
but you can change its properties, or add new ones. Think of it like this: you keep the same ID card, but you can update what's written on it.
Objects and arrays together
An array can hold objects, like a list of stories where each story is an object. A for...of loop from JS Lesson 4 can then visit each one.
An object can also hold arrays, like a team with a list of players.
constructionLet's build it
Let's describe a Front Page Bytes story as an object. The HTML and CSS stay the same in every step: a heading and an empty box, #card.
1Step 1: Make an object
Your code
const story = { title: "New phones arrive", author: "Ana", views: 120, isNew: true};console.log(story);
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div id="card">The story card will show here.</div> </body></html>
What you see
description(no title)
terminal
Console
Nothing here yet. console.log() writes here.
story.views = 150; changes a value that's already there. story.topic = "Tech"; adds a property that wasn't there before.
The console shows the updated object, with the new topic at the end.
4Step 4: Read with brackets
Your code
const story = { title: "New phones arrive", author: "Ana", views: 120, isNew: true};console.log(story["author"]);const field = "views";console.log(story[field]);
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div id="card">The story card will show here.</div> </body></html>
What you see
description(no title)
terminal
Console
Nothing here yet. console.log() writes here.
The properties are joined with HTML tags into one string, and innerHTML puts it in #card. The statement is split over three lines to make it easier to read. JavaScript reads it as one line, up to the ;.
6Step 6: A list of objects
Your code
const stories = [ { title: "New phones arrive", views: 120 }, { title: "Faster Wi-Fi for schools", views: 85 }, { title: "Robots help farmers", views: 230 }];for (const story of stories) { console.log(story.title + ": " + story.views + " views");}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div id="card">The story card will show here.</div> </body></html>
What you see
description(no title)
terminal
Console
Nothing here yet. console.log() writes here.
story.tilte has two letters swapped. JavaScript doesn't stop with an error. It looks for a property called tilte, doesn't find one, and gives undefined.
tips_and_updatesTip: Copy the key exactly as it's written in the object. Capital letters count too.
error
Forgetting a comma between properties
The mistake
const story = { title: "New phones arrive" author: "Ana"};console.log(story.title);
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <div id="card">The story card will show here.</div> </body></html>
What you see
description(no title)
terminal
Console
Nothing here yet. console.log() writes here.
Without the comma after line 2, JavaScript doesn't know where one property ends and the next begins. The whole script stops with a SyntaxError before anything runs.
tips_and_updatesTip: Put a comma after every property except the last one.
error
Using = instead of : inside an object
The mistake
const story = { title = "New phones arrive", author = "Ana"};console.log(story.title);