0
0
Javascriptprogramming~20 mins

Object keys and values in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Keys and Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1: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));
A["a", "b", "c"] and [1, 2, 3]
B[1, 2, 3] and ["a", "b", "c"]
C["a", "b", "c"] and ["1", "2", "3"]
D["1", "2", "3"] and [1, 2, 3]
Attempts:
2 left
πŸ’‘ Hint
Object.keys returns an array of property names, Object.values returns an array of property values.
❓ Predict Output
intermediate
1: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));
A["hidden", "visible"]
B[42, 7]
C["visible"]
D[]
Attempts:
2 left
πŸ’‘ Hint
Object.keys only lists enumerable properties.
❓ Predict Output
advanced
2: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));
A[["x", 10], ["y", "{z: 20}"]]
B[["x", 10], ["z", 20]]
C["x", 10, "y", {z: 20}]
D[["x", 10], ["y", {z: 20}]]
Attempts:
2 left
πŸ’‘ Hint
Object.entries returns an array of [key, value] pairs, values can be objects.
❓ Predict Output
advanced
1:30remaining
Output of Object.keys on array
What will this code output?
Javascript
const arr = ['a', 'b', 'c'];
console.log(Object.keys(arr));
A["0", "1", "2"]
B[0, 1, 2]
C["a", "b", "c"]
D[]
Attempts:
2 left
πŸ’‘ Hint
Arrays are objects with numeric keys as strings.
❓ Predict Output
expert
2: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));
A[1, 2]
B[1]
C[2]
D[]
Attempts:
2 left
πŸ’‘ Hint
Symbol keys are not included in Object.values.