Skip to content
FORGENO ADS GAMESUnlock all $8

JavaScript · Unit 3: Working with data · Lesson 3 of 6

Objects

An object groups related values under names called keys: const book = { title: "Dune", pages: 412 };

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

An object groups related values under names called keys: const book = { title: "Dune", pages: 412 };

Read a value with a dot: book.title. Change or add one the same way: book.pages = 500; book.read = true;

Arrays of objects are everywhere in real programs, like a list of products that each have a name and a price.

Worked example

JavaScript

const pet = { name: "Pip", kind: "rabbit", age: 2 };
console.log(pet.name);
pet.age = pet.age + 1;
console.log(`${pet.name} is ${pet.age}`);
console.log(pet);

It prints

Pip
Pip is 3
{name: 'Pip', kind: 'rabbit', age: 3}

Key terms

Object
Values grouped under named keys in { }. Read them with a dot: book.title.

Practise it in Code Academy

It has 4 exercises (predict the output, fill in the blank and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.