Bird
Raised Fist0
Javascriptprogramming~20 mins

Accessing object properties in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Accessing nested object properties
What is the output of the following code?
Javascript
const user = { name: 'Alice', details: { age: 30, city: 'Paris' } };
console.log(user.details.city);
A"Paris"
B"city"
Cundefined
DTypeError
Attempts:
2 left
πŸ’‘ Hint
Think about how to access properties inside nested objects using dot notation.
❓ Predict Output
intermediate
2:00remaining
Accessing object properties with bracket notation
What will this code output?
Javascript
const obj = { 'first-name': 'John', age: 25 };
console.log(obj['first-name']);
A"first-name"
Bundefined
C"John"
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint
Use bracket notation to access properties with special characters or hyphens.
❓ Predict Output
advanced
2:00remaining
Accessing properties with variables
What is the output of this code?
Javascript
const key = 'score';
const player = { name: 'Bob', score: 42 };
console.log(player[key]);
A"score"
B42
Cundefined
DReferenceError
Attempts:
2 left
πŸ’‘ Hint
When using a variable to access a property, use bracket notation.
❓ Predict Output
advanced
2:00remaining
Optional chaining with object properties
What will this code output?
Javascript
const data = { user: { profile: { name: 'Eve' } } };
console.log(data.user?.profile?.age);
A"age"
Bnull
CTypeError
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Optional chaining returns undefined if a property does not exist instead of throwing an error.
❓ Predict Output
expert
3:00remaining
Dynamic property access and computed property names
What is the output of this code?
Javascript
const prefix = 'user';
const id = 7;
const obj = { [`${prefix}${id}`]: 'active' };
console.log(obj.user7);
A"active"
BSyntaxError
Cundefined
DTypeError
Attempts:
2 left
πŸ’‘ Hint
Computed property names allow dynamic keys in object literals.

Practice

(1/5)
1. Which notation is used to access a property named age in a JavaScript object person when the property name is known and simple?
easy
A. person->age
B. person.age
C. person['age']
D. person(age)

Solution

  1. Step 1: Understand dot notation usage

    Dot notation is used when the property name is a simple, valid identifier known in advance.
  2. Step 2: Apply dot notation to access property

    Using person.age accesses the age property directly.
  3. Final Answer:

    person.age -> Option B
  4. Quick Check:

    Dot notation = person.age [OK]
Hint: Use dot notation for simple known property names [OK]
Common Mistakes:
  • Using arrow (->) instead of dot
  • Using parentheses like a function call
  • Using bracket notation unnecessarily
2. Which of the following is the correct syntax to access the property "first-name" of an object user?
easy
A. user['first-name']
B. user.first-name
C. user.first_name
D. user[first-name]

Solution

  1. Step 1: Identify property name with special characters

    The property name "first-name" contains a hyphen, which is not valid for dot notation.
  2. Step 2: Use bracket notation with quotes

    Bracket notation with quotes, user['first-name'], correctly accesses such properties.
  3. Final Answer:

    user['first-name'] -> Option A
  4. Quick Check:

    Bracket notation for special chars = user['first-name'] [OK]
Hint: Use bracket notation for property names with special characters [OK]
Common Mistakes:
  • Using dot notation with hyphenated names
  • Omitting quotes inside brackets
  • Using undefined variable inside brackets
3. What will be the output of the following code?
const car = { brand: 'Toyota', year: 2020 };
console.log(car['brand']);
medium
A. Error
B. brand
C. undefined
D. Toyota

Solution

  1. Step 1: Understand bracket notation access

    The code uses bracket notation with the string 'brand' to access the property value.
  2. Step 2: Retrieve the value of 'brand'

    The value of car['brand'] is 'Toyota', so it prints 'Toyota'.
  3. Final Answer:

    Toyota -> Option D
  4. Quick Check:

    car['brand'] = 'Toyota' [OK]
Hint: Bracket notation with string key returns property value [OK]
Common Mistakes:
  • Confusing property name with value
  • Expecting property name to print
  • Using wrong quotes causing error
4. Identify the error in the following code snippet:
const book = { title: '1984', author: 'Orwell' };
console.log(book[title]);
medium
A. Syntax error due to missing semicolon
B. Dot notation should be used instead
C. Missing quotes around 'title' in bracket notation
D. Property 'title' does not exist

Solution

  1. Step 1: Check bracket notation usage

    Inside brackets, property names must be strings or variables. Here, title is treated as a variable, not a string.
  2. Step 2: Fix by adding quotes

    Adding quotes: book['title'] correctly accesses the property.
  3. Final Answer:

    Missing quotes around 'title' in bracket notation -> Option C
  4. Quick Check:

    Bracket keys need quotes if not variables [OK]
Hint: Put quotes around property names in brackets unless variable [OK]
Common Mistakes:
  • Forgetting quotes around string keys
  • Assuming dot notation always required
  • Confusing variable and string keys
5. Given the object settings = { 'theme-color': 'dark', fontSize: 14 }, which code correctly changes the theme-color to 'light' and increases fontSize by 2?
hard
A. settings['theme-color'] = 'light'; settings.fontSize += 2;
B. settings.theme-color = 'light'; settings['fontSize'] += 2;
C. settings.theme-color = 'light'; settings.fontSize = settings.fontSize + 2;
D. settings['theme-color'] = 'light'; settings['fontSize'] =+ 2;

Solution

  1. Step 1: Update 'theme-color' using bracket notation

    Since 'theme-color' has a hyphen, use bracket notation: settings['theme-color'] = 'light';
  2. Step 2: Increase fontSize using dot notation

    fontSize is a simple property, so settings.fontSize += 2; correctly adds 2.
  3. Final Answer:

    settings['theme-color'] = 'light'; settings.fontSize += 2; -> Option A
  4. Quick Check:

    Bracket for special keys + dot for simple keys [OK]
Hint: Use bracket for special keys, dot for simple keys [OK]
Common Mistakes:
  • Using dot notation for hyphenated keys
  • Mistyping += as =+
  • Mixing up property names and strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard