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.
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.
}
When a Set contains an object, .has() determines whether or not the Set contains the same exact object, not whether the Set contains an object that contains the same data as some other object. It has to contain the same object.
So, .has(someObj) or .has(someArray) only returns true if that actual object is in the Set. It does not compare to see if a similar object with the same values/properties are in the Set, only if the actual object itself is in the Set.
It works similar to comparing two objects with === as in obj1 === obj2.
If you wanted something with features similar to a Set that would tell you if it had any other object matching the current one, then you'd have to write a different collection yourself and implement your own comparison algorithm. This is not how a Set or a Map work.
You can see some other discussion of a similar topic here: How to customize object equality for JavaScript Set
This should work:
const set = new Set();
const array = [ 2, 3 ];
set.add(1);
set.add(array);
set.add(array);
console.log(set);
console.log(set.has(array));
Set#has checks for object identity, not deep equality. This is different than, say, Java's HashMap, which uses Object#equals in the background.
According to docs:
The Set object lets you store unique values of any type, whether primitive values or object references.
As you are adding objects, their references will be stored internally and your check will return false because your are passing different object to has method.
Actually, your case is even covered in the Examples section of the has method reference.
In regards to objects, Sets handle unique references.
new Set([ {a: 1}, {a: 1} ]) has two items.
var x = {a: 1}; new Set([ x, x ]); has one item.
There is no way to have Set use a custom comparison function.