WeakMap

WeakMap.prototype.delete

Removes the specified element from the WeakMap by its key

Syntax

JavaScript
weakMap.delete(key)

Parameters

ParameterTypeDescription
keyobjectThe key of the element to remove

Return Value

true if the element existed and was removed, false otherwise

Examples

Basic Usage
const wm = new WeakMap()
const obj = {}
wm.set(obj, 42)
console.log(wm.delete(obj)) // true
console.log(wm.has(obj)) // false
Practical Example
const cache = new WeakMap<object, unknown>()
function invalidate(key: object) {
  const existed = cache.delete(key)
  console.log(existed ? 'Cache cleared' : 'Not cached')
}
Advanced Usage
const wm = new WeakMap<object, string>()
const key = {}
wm.set(key, 'temp')
wm.delete(key)
console.log(wm.get(key)) // undefined

Understanding WeakMap.prototype.delete

The WeakMap.prototype.delete method in JavaScript removes the specified element from the WeakMap by its key. It belongs to the WeakMap object and is one of the most widely used methods for working with weakmap values in modern JavaScript and TypeScript applications.

The method signature is weakMap.delete(key). It accepts 1 parameter: key. When called, it returns true if the element existed and was removed, false otherwise. Understanding when and how to use delete() helps you write more expressive, readable code.

Common use cases for WeakMap.prototype.delete include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like weakmap-set, weakmap-get, weakmap-has, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for WeakMap.prototype.delete is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More WeakMap Methods

Other methods in the WeakMap object

Related Tools

More WeakMap Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.