Skip to content

OutputNot attempted

Iterating a Map

const students = new Map([
['stu1', 'maths'],
['stu2', 'science'],
]);
for (const [id, subject] of students) console.log(id, subject);
const map = new Map([[150, 'abc'], [120, 'def']]);
console.log(map.entries().next().value, map.keys().next(), [...map.values()]);
console.log(map.size, map.get(150), map.get('150'));

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Maps iterate in INSERTION order (not sorted, unlike integer-like object keys). Iterating a Map gives [key, value] pairs. Keys are compared with SameValueZero, so 150 and '150' are different keys, which a plain object would merge.