Set.has() function in JavaScript


The has() function of the Set accepts a value and verifies whether the current object contains the specified value. If so, this function returns the boolean value true else, it returns false.

Syntax

Its Syntax is as follows

setObj.has()

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      const setObj = new Set();
      setObj.add('Java');
      setObj.add('JavaFX');
      setObj.add('JavaScript');
      setObj.add('HBase');
      document.write("Contents of the Set: ");
      document.write("<br>");
      for (let item of setObj) {
         document.write(item);
         document.write("<br>");
      }
      var result = setObj.has('JavaFX');
      if(result) {
         document.write("Set object contains specified element");
      }else {
         document.write("Set object contains specified element");
      }
   </script>
</body>
</html>

Output

Contents of the Set:
Java
JavaFX
JavaScript
HBase
trueSet object contains specified element

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 25-Jun-2020

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements