Open In App

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

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To check if an element exists in a Set in JavaScript, you can use the has() method. The has() method returns a boolean indicating whether the specified element is present in the Set.

Example: Here, mySet.has(3) returns true because the value 3 is present in the Set. On the other hand, mySet.has(6) returns false because 6 is not present in the Set.

Javascript




let mySet = new Set([1, 2, 3, 4, 5]);
 
console.log(mySet.has(3)); // Output: true
console.log(mySet.has(6)); // Output: false


Output

true
false

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads