0

I am printing a Map object before and after setting the map value. Before setting the map value, the map size is 0, which is correct, but the entries size is already 1.

script.js

const map = new Map();

console.log('Before calling map.set');
console.log(map);

console.log('After calling map.set');
map.set('key1', 'value1');
console.log(map);

Console

Before calling map.set
Map(0) {size: 0}
[[Entries]]
0: {"key1" => "value1"}
size: 1
[[Prototype]]: Map

After calling map.set
Map(1) {'key1' => 'value1'}
[[Entries]]
0: {"key1" => "value1"}
size: 1
[[Prototype]]: Map

I tried to map.get('key1') before calling map.set and it returns undefined correctly.

In the first console.log, I was expecting the entries size to be 0, similar to the map size as I have not called map.set('key1', 'value1'). I do not understand why the entries size is already set before the map value is set.

Ivone Djaja
  • 477
  • 6
  • 13
  • The `console()` API does weird things, especially in Chrome. – Pointy Mar 27 '22 at 12:14
  • 2
    The console logs a reference to your object. When it changes after having already been logged, the originally logged object also changes (when you expand it to view the Map's contents) – Nick Parsons Mar 27 '22 at 12:15

0 Answers0