Challenge - 5 Problems
Object Keys and Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate1:30remaining
Output of Object.keys and Object.values
What is the output of the following code?
Javascript
const obj = {a: 1, b: 2, c: 3}; console.log(Object.keys(obj)); console.log(Object.values(obj));
Attempts:
2 left
π‘ Hint
Object.keys returns an array of property names, Object.values returns an array of property values.
β Incorrect
Object.keys(obj) returns an array of the object's keys as strings. Object.values(obj) returns an array of the values in the same order.
β Predict Output
intermediate1:30remaining
Output of Object.keys with non-enumerable property
What will be logged to the console?
Javascript
const obj = {}; Object.defineProperty(obj, 'hidden', {value: 42, enumerable: false}); obj.visible = 7; console.log(Object.keys(obj));
Attempts:
2 left
π‘ Hint
Object.keys only lists enumerable properties.
β Incorrect
The property 'hidden' is non-enumerable, so Object.keys only returns ['visible'].
β Predict Output
advanced2:00remaining
Output of Object.entries with nested objects
What is the output of this code?
Javascript
const obj = {x: 10, y: {z: 20}}; console.log(Object.entries(obj));
Attempts:
2 left
π‘ Hint
Object.entries returns an array of [key, value] pairs, values can be objects.
β Incorrect
Object.entries returns an array of arrays, each with key and value. The value can be an object itself.
β Predict Output
advanced1:30remaining
Output of Object.keys on array
What will this code output?
Javascript
const arr = ['a', 'b', 'c']; console.log(Object.keys(arr));
Attempts:
2 left
π‘ Hint
Arrays are objects with numeric keys as strings.
β Incorrect
Object.keys on an array returns the indices as strings: ['0', '1', '2'].
β Predict Output
expert2:00remaining
Output of Object.values with symbol keys
What is the output of this code?
Javascript
const sym = Symbol('id'); const obj = {a: 1, [sym]: 2}; console.log(Object.values(obj));
Attempts:
2 left
π‘ Hint
Symbol keys are not included in Object.values.
β Incorrect
Object.values only returns values of string-keyed enumerable properties, symbol keys are ignored.