Challenge - 5 Problems
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this code using object property shorthand?
Consider the following JavaScript code snippet. What will be logged to the console?
Javascript
const name = 'Alice'; const age = 30; const person = { name, age }; console.log(person);
Attempts:
2 left
π‘ Hint
Object property shorthand lets you write { name, age } instead of { name: name, age: age }.
β Incorrect
The object person uses property shorthand, so the keys are 'name' and 'age' with values from the variables name and age.
β Predict Output
intermediate2:00remaining
What is the output when accessing nested object properties?
What will this code output to the console?
Javascript
const data = { user: { id: 1, info: { name: 'Bob', city: 'NY' } } }; console.log(data.user.info.city);
Attempts:
2 left
π‘ Hint
Access nested properties by chaining keys with dots.
β Incorrect
data.user.info.city accesses the city property inside info inside user, which is 'NY'.
β Predict Output
advanced2:00remaining
What is the output of this code using Object.assign?
What will be logged to the console after running this code?
Javascript
const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 }; const result = Object.assign(target, source); console.log(result);
Attempts:
2 left
π‘ Hint
Object.assign copies properties from source to target, overwriting existing keys.
β Incorrect
The property 'b' in target is overwritten by source's 'b', and 'c' is added.
β Predict Output
advanced2:00remaining
What error does this code raise when accessing a missing property?
What happens when this code runs?
Javascript
const obj = { x: 10 }; console.log(obj.y.z);
Attempts:
2 left
π‘ Hint
Accessing a property of undefined causes a TypeError.
β Incorrect
obj.y is undefined, so trying to access .z on undefined throws a TypeError.
β Predict Output
expert2:00remaining
What is the output of this code using computed property names?
What will this code print to the console?
Javascript
const key = 'score'; const player = { [key]: 100, ['level' + 1]: 2 }; console.log(player);
Attempts:
2 left
π‘ Hint
Square brackets let you use expressions as property names.
β Incorrect
The property names are computed: 'score' and 'level1'.