Stage 0 Draft / July 9, 2026

Map.prototype.take and WeakMap.prototype.take

Introduction

This proposal adds take methods to Map.prototype and WeakMap.prototype. A call to take(_key_) removes the entry for key (if any) and returns the value that was associated with it, or undefined if the map had no entry for key. It is the atomic combination of get(_key_) followed by delete(_key_), expressed as a single lookup.

The new methods are inserted into the alphabetical listings of the existing prototype clauses: Map.prototype.take between Map.prototype.set and Map.prototype.values, and WeakMap.prototype.take after WeakMap.prototype.set. The surrounding, unchanged clauses are elided here.

Editor's Note

The algorithms below deliberately mirror the existing Map.prototype.get / Map.prototype.delete and WeakMap.prototype.get / WeakMap.prototype.delete pairs so that take composes their observable behaviour without introducing any new abstract operations.

1 Map Objects

1.1 Properties of the Map Prototype Object

1.1.1 Map.prototype.take ( key )

This method performs the following steps when called:

  1. Let map be the this value.
  2. Perform ? RequireInternalSlot(map, [[MapData]]).
  3. Set key to CanonicalizeKeyedCollectionKey(key).
  4. For each Record { [[Key]], [[Value]] } entry of map.[[MapData]], do
    1. If entry.[[Key]] is not empty and SameValue(entry.[[Key]], key) is true, then
      1. Let value be entry.[[Value]].
      2. Set entry.[[Key]] to empty.
      3. Set entry.[[Value]] to empty.
      4. Return value.
  5. Return undefined.
Note 1

take returns undefined both when key was absent and when key was present with the value undefined. As with get, use has beforehand to distinguish these cases.

Note 2

The value empty is used as a specification device to indicate that an entry has been deleted. Actual implementations may take other actions such as physically removing the entry from internal data structures.

2 WeakMap Objects

2.1 Properties of the WeakMap Prototype Object

2.1.1 WeakMap.prototype.take ( key )

This method performs the following steps when called:

  1. Let weakMap be the this value.
  2. Perform ? RequireInternalSlot(weakMap, [[WeakMapData]]).
  3. If CanBeHeldWeakly(key) is false, return undefined.
  4. For each Record { [[Key]], [[Value]] } entry of weakMap.[[WeakMapData]], do
    1. If entry.[[Key]] is not empty and SameValue(entry.[[Key]], key) is true, then
      1. Let value be entry.[[Value]].
      2. Set entry.[[Key]] to empty.
      3. Set entry.[[Value]] to empty.
      4. Return value.
  5. Return undefined.
Note 1

A value that cannot be held weakly can never have been used as a key, so take returns undefined for such key without inspecting [[WeakMapData]], matching WeakMap.prototype.get.

Note 2

The value empty is used as a specification device to indicate that an entry has been deleted. Actual implementations may take other actions such as physically removing the entry from internal data structures.

Copyright & Software License

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.