The has() method of the Set object in JavaScript returns a boolean asserting whether an element with the given value is present in the Set object or not.
The syntax of Set.has() method is:
has(value)
This has() method of Set returns a boolean asserting whether an element is present with the given value in the Set object or not.
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value of the element to test for presence in the Set. |
Set.has() returns value of type boolean
.
In JavaScript, we can use the has()
method to check if a specific value is present in a Set object.
For example,
letters
with initial values 'a', 'b', and 'c'.has()
method to check if the value 'b' is present in the letters
Set.console.log()
.const letters = new Set(['a', 'b', 'c']);
const hasB = letters.has('b');
console.log(hasB);
true
In JavaScript, we can use the has()
method to check if a value that is not present in the Set object returns false.
For example,
numbers
with initial values 1, 2, and 3.has()
method to check if the value 4 is present in the numbers
Set.console.log()
.const numbers = new Set([1, 2, 3]);
const hasFour = numbers.has(4);
console.log(hasFour);
false
In JavaScript, we can use the has()
method to check if an object is present in a Set object.
For example,
people
with initial objects representing different people.has()
method to check if a specific person object is present in the people
Set.console.log()
.const person1 = { name: 'John' };
const person2 = { name: 'Jane' };
const people = new Set([person1, person2]);
const hasJohn = people.has(person1);
console.log(hasJohn);
true
In this JavaScript tutorial, we learned about has() method of Set: the syntax and few working examples with output and detailed explanation for each example.