Map.prototype.has
Returns a boolean indicating whether a value associated with the specified key exists in the Map or not
Syntax
map.has(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | K | The key to check for |
Return Value
true if an element with the specified key exists, false otherwise
Examples
const map = new Map([['a', 1], ['b', 2]]);
console.log(map.has('a')); // true
console.log(map.has('c')); // falsefunction getOrDefault<K, V>(map: Map<K, V>, key: K, def: V): V {
return map.has(key) ? map.get(key)! : def;
}const visited = new Map<string, boolean>();
function visit(url: string) {
if (!visited.has(url)) {
visited.set(url, true);
console.log(`Visiting ${url}`);
}
}Understanding Map.prototype.has
The Map.prototype.has method in JavaScript returns a boolean indicating whether a value associated with the specified key exists in the Map or not. It belongs to the Map object and is one of the most widely used methods for working with map values in modern JavaScript and TypeScript applications.
The method signature is map.has(key). It accepts 1 parameter: key. When called, it returns true if an element with the specified key exists, false otherwise. Understanding when and how to use has() helps you write more expressive, readable code.
Common use cases for Map.prototype.has include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like map-get, map-set, map-delete, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Map.prototype.has 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
Map.prototype.getReturns the value associated to the specified key, or undefined if there is no corresponding entry
Map.prototype.setAdds or updates an entry in a Map object with a specified key and value
Map.prototype.deleteRemoves the specified element from a Map object by key
Set.prototype.hasReturns a boolean indicating whether an element with the specified value exists in a Set object or not
More Map Methods
Other methods in the Map object
Related Tools
More Map Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.