0
0
Javascriptprogramming~20 mins

Why objects are needed in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use objects to group data?

Imagine you want to store information about a book: its title, author, and year published. Why is using an object helpful compared to separate variables?

AObjects automatically print data without needing code.
BObjects prevent any changes to the data once created.
CObjects let you group related data together, making it easier to manage and pass around.
DObjects make the program run faster by default.
Attempts:
2 left
πŸ’‘ Hint

Think about how you would carry multiple pieces of information together.

❓ Predict Output
intermediate
2:00remaining
What is the output of this code using an object?

Look at this JavaScript code that uses an object to store a person's info. What will it print?

Javascript
const person = { name: 'Anna', age: 30 };
console.log(person.name + ' is ' + person.age + ' years old.');
Aperson.name is 30 years old.
BAnna is 30 years old.
Cundefined is undefined years old.
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint

Access object properties with dot notation.

❓ Predict Output
advanced
2:00remaining
What happens if you try to access a missing property?

What will this code print when trying to access a property that does not exist in the object?

Javascript
const car = { brand: 'Toyota', year: 2020 };
console.log(car.color);
Aundefined
Bnull
CError: Property not found
D'' (empty string)
Attempts:
2 left
πŸ’‘ Hint

Think about what JavaScript returns when a property is missing.

🧠 Conceptual
advanced
2:00remaining
Why are objects better than arrays for named data?

You want to store a person's name and age. Why is an object better than an array for this?

AArrays are slower to create than objects.
BObjects automatically sort data alphabetically.
CArrays cannot store numbers.
DObjects use named keys, so you can access data by meaningful names like 'name' and 'age'.
Attempts:
2 left
πŸ’‘ Hint

Think about how you find data by position or by name.

❓ Predict Output
expert
2:00remaining
What is the output of this nested object access?

Consider this object with nested objects. What will the code print?

Javascript
const user = {
  id: 1,
  profile: {
    username: 'coder',
    details: {
      age: 25
    }
  }
};
console.log(user.profile.details.age);
A25
Bundefined
CError: Cannot read property 'age' of undefined
Dnull
Attempts:
2 left
πŸ’‘ Hint

Access nested properties step by step using dot notation.