Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to know whether a value is searched in Javascript sets?
In JavaScript, sets are collections of unique values. Sometimes you need to check whether a specific element exists inside a set. In this article, we will explore different techniques to verify element presence in JavaScript sets.
Using for Loop (Manual Search)
You can search for an element by iterating through the set and comparing each element with the target value. If found, return true; otherwise, return false after checking all elements.
<!DOCTYPE html>
<html>
<head>
<title>HTML Console</title>
</head>
<body>
<h3>Output Console</h3>
<p>Output:</p>
<div id="output"></div>
<script>
const mySet = new Set(['one', 'two', 3, 4, 5, 'SIX', '7']);
function searchInSet(set, key) {
for (const element of set) {
if (element === key) {
return true;
}
}
return false;
}
let output = "";
output += "Is 4 present in the set: " + searchInSet(mySet, 4) + "<br>";
output += "Is '7' present in the set: " + searchInSet(mySet, '7') + "<br>";
output += "Is 40 present in the set: " + searchInSet(mySet, 40) + "<br>";
document.getElementById('output').innerHTML = output;
</script>
</body>
</html>
Is 4 present in the set: true Is '7' present in the set: true Is 40 present in the set: false
Using has() Method (Recommended)
JavaScript Set objects provide a built-in has() method that efficiently checks whether a given element exists in the set. It returns true if the element is present, otherwise false.
Syntax
set_object.has(key)
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Console</title>
</head>
<body>
<h3>Output Console</h3>
<p>Output:</p>
<div id="output"></div>
<script>
const s = new Set();
s.add('java');
let output = "";
output += "Is java present? " + s.has('java') + "<br>";
output += "Is javascript present? " + s.has('javascript') + "<br>";
// Example with objects
const s1 = new Set();
const ob1 = { 'c++': 1 };
s1.add(ob1);
output += "ob1 is in the set? " + s1.has(ob1) + "<br>";
output += "New object {'c++': 1} is in the set? " + s1.has({ 'c++': 1 }) + "<br>";
document.getElementById('output').innerHTML = output;
</script>
</body>
</html>
Is java present? true
Is javascript present? false
ob1 is in the set? true
New object {'c++': 1} is in the set? false
Key Points
When working with objects in sets, remember that JavaScript uses reference equality. Two objects with identical properties are considered different if they are separate object instances.
Comparison
| Method | Performance | Readability | Recommended |
|---|---|---|---|
| for loop | O(n) - slower | More code | No |
| has() method | O(1) - faster | Clean and simple | Yes |
Conclusion
Use the has() method as the standard way to check for element presence in JavaScript sets. It's more efficient and readable than manual iteration approaches.
