Computer >> Computer tutorials >  >> Programming >> Javascript

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

<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