Set

Set.prototype.has

Returns a boolean indicating whether an element with the specified value exists in a Set object or not

Syntax

JavaScript
set.has(value)

Parameters

ParameterTypeDescription
valueTThe value to test for

Return Value

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

Examples

Basic Usage
const set = new Set([1, 2, 3, 4, 5]);
console.log(set.has(3)); // true
console.log(set.has(6)); // false
Practical Example
const visited = new Set<string>();
function visit(page: string) {
  if (!visited.has(page)) {
    visited.add(page);
    console.log(`New page: ${page}`);
  }
}
Advanced Usage
const allowedRoles = new Set(['admin', 'editor', 'viewer']);
function hasAccess(role: string) {
  return allowedRoles.has(role);
}
console.log(hasAccess('admin')); // true

Understanding Set.prototype.has

The Set.prototype.has method in JavaScript returns a boolean indicating whether an element with the specified value exists in a Set object or not. It belongs to the Set object and is one of the most widely used methods for working with set values in modern JavaScript and TypeScript applications.

The method signature is set.has(value). It accepts 1 parameter: value. When called, it returns true if an element with the specified value exists, false otherwise. Understanding when and how to use has() helps you write more expressive, readable code.

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

Browser support for Set.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 Set Methods

Other methods in the Set object

Related Tools

More Set Methods

Explore JavaScript Methods

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