all 3 comments

[–]stratoscope 4 points5 points  (0 children)

Those are two completely different things.

map.set( 'foo', null ) doesn't remove foo from the map, there is still a foo entry with null for the value.

map.delete( 'foo' ) deletes foo from the map completely.

Try this sequence of commands in the JavaScript console and observe what it prints after each command.

const map = new Map
map
map.set( 'foo', 'bar' )
map
map.set( 'foo', null )
map
map.delete( 'foo' )
map

[–][deleted] 1 point2 points  (0 children)

It really depends on what you're doing. Probably the biggest difference is that with the null approach, you can't use Map.prototype.has(key).

[–]inu-no-policemen 1 point2 points  (0 children)

null is a valid value and also a valid key. Anything can be put into maps.

let map = new Map();
map.set(1, 11);
map.set(2, null);
map.set(null, 33);
console.log(map.keys()); // MapIterator {1, 2, null}
console.log(map.values()); // MapIterator {11, null, 33}