Open In App

How to Check if a Specific Element Exists in a Set in JavaScript ?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To check if a specific element exists in a Set in JavaScript, you can use the has method. The has method returns a boolean indicating whether an element with the specified value exists in the Set

Syntax:

myset.has(value);

Parameters:

  • value: It is the value of the element that has to be checked if it exists in the Set.

Example 1: Here, we have checked whether “yellow” exists in tempset or not, as it is present we got the output as true.

The has( ) method takes one value as a parameter and checks whether it is present in the set given or not.

Javascript




let tempset =new Set();
 
tempset.add("red");
tempset.add("green");
tempset.add("blue");
tempset.add("yellow");
tempset.add("digitalvasanth");
 
 
console.log(tempset.has("yellow"));


Output

true

Example 2: Here, we have passed 99 to has( ) , which is not present in tempset, so it returned false as output.

Javascript




let tempset =new Set();
 
tempset.add(80);
tempset.add(88);
tempset.add(90);
tempset.add(77);
 
 
console.log(tempset.has(99));


Output

false

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads