๐ Scenario: Imagine you run a small bookstore. You want to keep track of books with their title, author, and price. Using separate variables for each book detail is confusing and hard to manage. Objects help group related information together, just like a real book has all its details in one place.
๐ฏ Goal: You will create an object to store book details, add a new property for stock count, and then print the full book information. This shows why objects are useful to keep related data organized.
๐ What You'll Learn
Create an object called book with properties title, author, and price with exact values
Add a new property stock to the book object with a number value
Use a for...in loop to go through all properties of book
Print each property and its value in the format: property: value
๐ก Why This Matters
๐ Real World
Objects are used everywhere in programming to represent real things with many details, like books, users, or products.
๐ผ Career
Understanding objects is essential for web development, as JavaScript uses objects to handle data, user info, and more.
Progress0 / 4 steps
1
Create the book object with details
Create an object called book with these exact properties and values: title set to "JavaScript Basics", author set to "Jane Doe", and price set to 29.99.
Javascript
Hint
Use curly braces {} to create an object and separate properties with commas.
2
Add a stock property to the book object
Add a new property called stock to the existing book object and set it to the number 12.
Javascript
Hint
Use dot notation to add a new property to an existing object.
3
Use a for...in loop to access all properties of book
Write a for...in loop using the variable key to go through all properties of the book object.
Javascript
Hint
Use for (const key in book) to loop over all keys in the object.
4
Print each property and its value
Inside the for...in loop, write a console.log statement to print each property and its value in the format: property: value using key and book[key].
Javascript
Hint
Use a template string with backticks and ${} to insert variables inside the string.
Practice
(1/5)
1. Why do we use objects in JavaScript? const person = { name: 'Alice', age: 25 };
easy
A. To group related data under one name
B. To perform mathematical calculations
C. To create loops
D. To write comments
Solution
Step 1: Understand the purpose of objects
Objects group related data, like name and age, under one variable.
Step 2: Compare with other options
Options B, C, and D describe other programming tasks, not grouping data.
Final Answer:
To group related data under one name -> Option A
Quick Check:
Objects group data = A [OK]
Hint: Objects hold related info together [OK]
Common Mistakes:
Thinking objects are for calculations
Confusing objects with loops
Believing objects are for comments
2. Which of the following is the correct way to create an object in JavaScript?
easy
A. const obj = [name: 'John', age: 30];
B. const obj = (name: 'John', age: 30);
C. const obj = { name: 'John', age: 30 };
D. const obj = ;
Solution
Step 1: Recall object syntax in JavaScript
Objects use curly braces {} with key: value pairs inside.
Hint: Objects use curly braces with key:value pairs [OK]
Common Mistakes:
Using parentheses instead of braces
Using square brackets for objects
Using angle brackets incorrectly
3. What will be the output of this code?
const car = { brand: 'Toyota', year: 2020 };
console.log(car.brand);
medium
A. car.brand
B. 2020
C. undefined
D. 'Toyota'
Solution
Step 1: Access the object property
car.brand accesses the value of the brand key in the car object.
Step 2: Identify the value stored
The brand key has the value 'Toyota', so console.log outputs 'Toyota'.
Final Answer:
'Toyota' -> Option D
Quick Check:
car.brand = 'Toyota' [OK]
Hint: Access object properties with dot notation [OK]
Common Mistakes:
Printing the key name instead of value
Confusing property access syntax
Expecting the year instead of brand
4. Find the error in this object creation:
const book = { title: 'JS Guide', pages = 300 };
medium
A. Using '=' instead of ':' for key-value
B. Object keys must be strings
C. Missing comma between properties
D. Missing semicolon after object
Solution
Step 1: Check key-value syntax in object
Object properties use colon ':' to separate key and value, not '='.
Step 2: Identify the error in code
The code uses pages = 300, which is incorrect syntax for objects.
Final Answer:
Using '=' instead of ':' for key-value -> Option A
Quick Check:
Key:value uses ':' not '=' [OK]
Hint: Use colon ':' between key and value in objects [OK]
Common Mistakes:
Using '=' instead of ':'
Forgetting commas between properties
Thinking keys must be quoted always
5. You want to store information about a student including name, age, and grades for math and science. Which is the best way to organize this data using objects?