Open In App

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

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:

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.




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.




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

Output
false
Article Tags :