0
0
Javascriptprogramming~15 mins

Why objects are needed in Javascript - See It in Action

Choose your learning style9 modes available
Why Objects Are Needed in JavaScript
πŸ“– 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a template string with backticks and ${} to insert variables inside the string.