Javascript Set has()

Introduction

The Javascript Set has() method checks existence of an element in the Set.

It returns a boolean value.

mySet.has(value);
  • value - the value to test for presence in the Set object.

Using the has method

var mySet = new Set();
mySet.add('foo');

let a = mySet.has('foo');
console.log(a);/* w w w  .j a  v a 2  s.c o m*/
mySet.has('bar');
console.log(a);

var set1 = new Set();
var obj1 = {'key1': 1};
set1.add(obj1);

a = set1.has(obj1);
console.log(a);
a = set1.has({'key1': 1});
console.log(a);// returns false because they have different object references
set1.add({'key1': 1});
console.log(set1);



PreviousNext

Related