JavaScript Set has() Method Tutorial

In this section, we will learn what the Set has() method is and how to use it in JavaScript.

What is JavaScript Set has() Method?

When we want to check whether a value is already in a `Set` object, we use the `has()` method.

JavaScript Set has() Method Syntax:

has(value)

has() Function Parameters:

The method takes one argument, and that is the target value we want to check a Set object for.

has() Function Return Value:

The return value of this method is a boolean.

If the target Set object has the argument value, then the result of this method is true.

Otherwise, the result will be false.

Example: using Set has() method in JavaScript

const collect = new Set();
collect.add("Jack");
collect.add("Jack");
collect.add("Omid");
collect.add("John");

if (collect.has("Jack")){
  console.log("The value 'Jack' is in this collection");
}
if (collect.has("Jason")){
  console.log("The value 'Jason' is in this collection");
}else{
  console.log("The value 'Jason' is not in this collection");
}

Output:

The value 'Jack' is in this collection

The value 'Jason' is not in this collection
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies