javascript · level 2 · unit 5 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps related values together, each with a label.
An array is a numbered list. Often you want labels instead: a pet's name, age and favourite toy. An object holds those values together, each under its own , like a form with a labelled box for every answer.
Write an object in curly brackets: { name: "Rex", age: 3 }. Each key is followed by a colon and its value, and commas separate the pairs. Read a value with a dot and its key: pet.name.
The event your listeners catch is an object too. That is why event.target works.
const pet = { name: "Rex", age: 3, toy: "squeaky ball" };
console.log(pet.name);
console.log(`${pet.name} is ${pet.age} and loves his ${pet.toy}.`);
console.log(pet); The console shows a whole object with every key in double quotes. That is how Gwecode prints objects.
guess first
A film in an object. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
const film = { title: "Paddington", year: 2014 };
console.log(`${film.title} came out in ${film.year}.`);
console.log(film);
your turn
Your turn. Describe your dream car.
Type a colour between the quotes and a number of seats after it. In the last blank, type the key that reads the seats. Press run.
fix it
The library card crashes. Fix it!
- A SyntaxError means JavaScript cannot read line 1. Look between the two pairs.
- Every key and value pair needs something to separate it from the next.
- Add a comma after "Matilda" on line 1, then press run.
nailed it
you packed values into an object. :D
- An object holds values, each under its own key.
- Write
{ key: value, key: value }in curly brackets. - Read a value with a dot, like
pet.name.
new words in your collection
- object
- key
This lesson isn’t saved. A free account remembers every lesson you finish.