Default value argument for Map/WeakMap.prototype.get()

Jason Orendorff jason.orendorff at
Wed Nov 28 11:07:56 PST 2012

I propose adding an optional second argument to the .get() method on Map
and WeakMap.

    Map.prototype.get(key[, defaultValue])

The default value would be returned only when the given key is not in the
map. That is, in the last step of the spec for these two methods, instead
of "Return undefined", it would say "Return defaultValue".

Python and Ruby have this. (In Ruby it's called "fetch".) It's handy in
cases like:

    var counts = new Map;
    for (let word of words)
        counts.set(word, counts.get(word, 0) + 1);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list