Predict the output of the following javascript code
const obj = { a: 1, b: 2, c: 3 };
let result = "";
for (const [key, value] of Object.entries(obj)) {
result += key + value;
}
console.log(result);
Quiz Explanation
This JavaScript code iterates over the properties of the object obj
using a for...of
loop combined with Object.entries()
to get an array of key-value pairs. Inside the loop, it concatenates each key and its corresponding value to the result
string.
So, the output of this code will be the concatenation of keys and values of the object obj
, resulting in:
a1b2c3
This output combines each key with its corresponding value in the object obj
.