Challenge - 5 Problems
Object Entries Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of Object.entries on a simple object?
Consider the following code snippet. What will be printed to the console?
Javascript
const obj = {a: 1, b: 2, c: 3}; console.log(Object.entries(obj));
Attempts:
2 left
π‘ Hint
Object.entries returns an array of [key, value] pairs.
β Incorrect
Object.entries returns an array where each element is a two-item array: the first is the key as a string, the second is the value. So for {a:1, b:2, c:3}, it returns [['a',1], ['b',2], ['c',3]].
β Predict Output
intermediate2:00remaining
What does Object.entries return for an object with symbol keys?
What will be the output of the following code?
Javascript
const sym = Symbol('id'); const obj = {a: 10, [sym]: 20}; console.log(Object.entries(obj));
Attempts:
2 left
π‘ Hint
Object.entries only includes string keys, not symbol keys.
β Incorrect
Object.entries returns only enumerable string-keyed properties. Symbol keys are ignored, so only [['a', 10]] is returned.
β Predict Output
advanced2:00remaining
Output of Object.entries on an object with inherited properties
What will this code print?
Javascript
const parent = {x: 1}; const child = Object.create(parent); child.y = 2; console.log(Object.entries(child));
Attempts:
2 left
π‘ Hint
Object.entries only lists own enumerable properties, not inherited ones.
β Incorrect
Object.entries returns only the object's own enumerable properties. 'y' is own property, 'x' is inherited, so only [['y', 2]] is returned.
β Predict Output
advanced2:00remaining
What is the output of Object.entries on an array?
What will this code print?
Javascript
const arr = ['a', 'b', 'c']; console.log(Object.entries(arr));
Attempts:
2 left
π‘ Hint
Arrays are objects with keys as string indices.
β Incorrect
Arrays are objects with keys as string indices. Object.entries returns these index-value pairs as [['0','a'], ['1','b'], ['2','c']].
π§ Conceptual
expert2:00remaining
Which option causes a TypeError when using Object.entries?
Which of the following code snippets will cause a TypeError when calling Object.entries?
Attempts:
2 left
π‘ Hint
Object.entries expects an object, null is not an object.
β Incorrect
Object.entries throws a TypeError if the argument is null or undefined because they are not objects. Other options are valid inputs.