JavaScript Tutorial

JavaScript Set has() Method: Check if a Value Exists

TGTeam GyaniPanditNovember 12, 20263 min read

Hello friends! Very often in a program we need to ask a simple question: “is this item already in my list?” If the list is a JavaScript Set, the answer comes from has(). The has() method of a JavaScript Set checks whether a value is present in the set. It returns true or false and never changes the set.

set.has(value)

It is the Set’s answer to “is this item already here?”, and it is much faster than searching an array when you have many items. Let’s see how it works.

Basic Example

const fruits = new Set(["apple", "mango", "banana"]);
console.log(fruits.has("mango"));
console.log(fruits.has("Mango"));
console.log(fruits.has("grapes"));

Output:

true
false
false

The check is exact, so "Mango" with a capital M is a different value from "mango". If case should not matter in your program, store lower-case values and check with value.toLowerCase().

But what exactly does “exact” mean here? That is the next thing to understand.

How has() Compares Values

has() uses a rule called SameValueZero. It works like ===, with one useful difference: NaN is treated as equal to NaN. Also, 0 and -0 count as the same value.

const s = new Set([1, "2", NaN, 0]);
console.log(s.has(1), s.has("1"), s.has(2), s.has("2"));
console.log(s.has(NaN));
console.log(s.has(-0));

Output:

true false false true
true
true

There is no type conversion: the number 1 and the string "1" are different values.

has() with Objects

This is where many beginners get confused. Objects are compared by reference, not by their contents. A new object that looks the same is still a different object:

const user = { name: "Riya" };
const users = new Set([user]);
console.log(users.has(user));
console.log(users.has({ name: "Riya" }));

Output:

true
false

If you need to look up objects by their content, store an id in the set instead (for example new Set([user.id])), or use a Map keyed by id.

Practical Example: Skipping Duplicates

Now let’s use has() in a small real-world task: keeping track of pages a user has already visited.

const visited = new Set();
for (const page of ["home", "about", "home", "contact", "about"]) {
  if (visited.has(page)) {
    console.log("already seen:", page);
  } else {
    visited.add(page);
  }
}
console.log(visited.size);

Output:

already seen: home
already seen: about
3

Set.has() vs Array.includes()

You might be thinking, arrays also have includes(), so why use a Set at all? Here is a quick comparison:

Set.has() Array.includes()
Speed on large data Fast, roughly constant time Slower, checks items one by one
Finds NaN Yes Yes (but indexOf does not)
Duplicates allowed No Yes
Best for Repeated membership checks Small lists, or when order and duplicates matter
const arr = ["a", "b", NaN];
console.log(arr.includes(NaN), arr.indexOf(NaN));

Output:

true -1

If your code checks the same list again and again (inside a loop, for example), build a Set once and use has().

So friends, remember this: has() always returns a simple true or false, it matches values exactly, and it checks objects by reference. Use it whenever your code needs to ask “have I seen this before?” again and again.

Related: Sets in JavaScript · Set add() · Set delete() · Set size

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses