WeakMap.prototype.delete
Removes the specified element from the WeakMap by its key
Syntax
weakMap.delete(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | object | The key of the element to remove |
Return Value
true if the element existed and was removed, false otherwise
Examples
const wm = new WeakMap()
const obj = {}
wm.set(obj, 42)
console.log(wm.delete(obj)) // true
console.log(wm.has(obj)) // falseconst cache = new WeakMap<object, unknown>()
function invalidate(key: object) {
const existed = cache.delete(key)
console.log(existed ? 'Cache cleared' : 'Not cached')
}const wm = new WeakMap<object, string>()
const key = {}
wm.set(key, 'temp')
wm.delete(key)
console.log(wm.get(key)) // undefinedUnderstanding 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
WeakMap.prototype.setAdds or updates an element with a specified key and value to a WeakMap where the key must be an object or symbol
WeakMap.prototype.getReturns the value associated with the specified key in the WeakMap, or undefined if the key is not present
WeakMap.prototype.hasReturns a boolean indicating whether an element with the specified key exists in the WeakMap
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.