Set.add() function in JavaScript


The add() function of the Set object accepts a value and adds/appends it to the current Set object.

Syntax

Its Syntax is as follows

setObj.add()

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>");
      }
   </script>
</body>
</html>

Output

Contents of the Set:
Java
JavaFX
JavaScript
HBase

Updated on: 25-Jun-2020

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements