Map

Map.prototype.has

Returns a boolean indicating whether a value associated with the specified key exists in the Map or not

Syntax

JavaScript
map.has(key)

Parameters

ParameterTypeDescription
keyKThe key to check for

Return Value

true if an element with the specified key exists, false otherwise

Examples

Basic Usage
const map = new Map([['a', 1], ['b', 2]]);
console.log(map.has('a')); // true
console.log(map.has('c')); // false
Practical Example
function getOrDefault<K, V>(map: Map<K, V>, key: K, def: V): V {
  return map.has(key) ? map.get(key)! : def;
}
Advanced Usage
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

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.