Level Navigation: 0 | 1 | Current Level: 2 | 3 | 4 | 5 |
Learn to identify keys and values, create your own POJOs, explain object representations, and fix syntax errors.
Before we dive into the exercises, letโs understand the important terms weโll be using:
A property is a piece of information stored in an object. Think of it like a characteristic or attribute of something.
Example:
const car = {
"make": "Toyota", // This is a property
"model": "Corolla", // This is a property
"year": 2020 // This is a property
};
A key is the name of a property. Itโs like a label that tells us what information weโre storing.
Example:
const pet = {
"name": "Buddy", // "name" is the key
"species": "Dog", // "species" is the key
"age": 5 // "age" is the key
};
A value is the actual data stored in a property. It can be a string, number, boolean, or other data type.
Example:
const book = {
"title": "JavaScript for Beginners", // "JavaScript for Beginners" is the value
"author": "Jane Doe", // "Jane Doe" is the value
"pages": 250, // 250 is the value
"isPublished": true // true is the value
};
Together, a key and its value form a key-value pair:
"key": "value"
In the example above:
"title"
is the key"JavaScript for Beginners"
is the value"title": "JavaScript for Beginners"
This is a completed example of a POJO with comments explaining each part.
const car = {
"make": "Toyota",
"model": "Corolla",
"year": 2020
};
console.log(car.make); // Prints: Toyota
console.log(car.model); // Prints: Corolla
console.log(car.year); // Prints: 2020
// Now let's update the year
car.year = 2024;
console.log(car.year); // Prints: 2024
// Summary comment:
// - car is a POJO with three properties: make, model, year.
// - We used dot notation to read the properties.
// - We updated the year from 2020 to 2024.
const book = {
"title": "JavaScript for Beginners",
"author": "Jane Doe",
"pages": 250,
"isPublished": true
};
// TODO: List all the keys as a comment
// TODO: List all the values as a comment
const pet = {
"name": "Buddy",
"species": "Dog",
"age": 5
};
console.log(pet);
// TODO: What are the values for name, species, and age? Write as comments.
const sprite = {
"name": "Hero",
"x": 100,
"y": 200
};
console.log(sprite);
// TODO: What does this object represent? Write your explanation as a comment.
const car = {
"make": "Toyota",
"model": "Corolla",
"year": 2020
"color": "Blue"
};
// TODO: Fix the syntax error.
// TODO: Explain what was wrong in a comment.
console.log(car);
git add script.js
git commit -m "Step 2 - Exercises completed"
git push
You learned:
Level Navigation: 0 | 1 | Current Level: 2 | 3 | 4 | 5 |