You can't compare references like arrays and objects for equality (you can compare values, though).
The way you're doing it, you're checking against a different reference, even though the values appear to be the same.
Do something like this:
var a = new Set();
var b = [1];
a.add(b);
a.has(b); // => true
Take a look at MDN's Equality comparisons and sameness.
So it might be that the Set .has() is using == for comparing equality (not sure though)
Not necessarily. [1] === [1] (using the strict equality operator) also returns false.
Is there a .contains() method for Set() in JS like Java has?
Not in the way you're imagining it. You can do some sort of deep comparison, as mentioned in the first link in this answer.
Answer from Josh Beam on Stack OverflowYou can't compare references like arrays and objects for equality (you can compare values, though).
The way you're doing it, you're checking against a different reference, even though the values appear to be the same.
Do something like this:
var a = new Set();
var b = [1];
a.add(b);
a.has(b); // => true
Take a look at MDN's Equality comparisons and sameness.
So it might be that the Set .has() is using == for comparing equality (not sure though)
Not necessarily. [1] === [1] (using the strict equality operator) also returns false.
Is there a .contains() method for Set() in JS like Java has?
Not in the way you're imagining it. You can do some sort of deep comparison, as mentioned in the first link in this answer.
although both [1]'s have the same value, they are two different arrays, pointing to two different locations in memory, hence they are not equal.
you can use a basic loop to check
for (let item of mySet.keys()) {
item.toString() == [1].toString();
//true on match.
}