How to know whether a value is searched in Javascript sets?

JavascriptFront End TechnologyObject Oriented Programming

The Set class in JavaScript provides a has method to search elements in a given set object. This method can be used as follows −

Example

let mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(1);
mySet.add(3);
mySet.add("a");
console.log(mySet)
console.log(mySet.has(55))
console.log(mySet.has(""))
console.log(mySet.has({}))
console.log(mySet.has(1))
console.log(mySet.has("a"))

Output

Set { 1, 2, 3, 'a' }
false
false
false
true
true
raja
Published on 18-Sep-2019 12:12:59
Advertisements