ES6 - Set Method add())



This function is used to add an element to the set.

Syntax

The below mentioned syntax is for add(), where, value is the value to add to the Set.

set_name.add(value)

Example

<script>
   let s = new Set([{
      ename:'Smith'
   },{
      ename:'Kannan'
   }])
   console.log(s)

   let students =new Set();
   students.add('Varun');
   students.add('Prijin');
   students.add('Navya');
   students.add('Kannan') //chaining
      .add('Raj')
      .add('Koshy')
      .add('Sudhakaran');
   console.log(students)
</script>

The output of the above code is as given below −

{{…}, {…}}
{"Varun", "Prijin", "Navya", "Kannan", "Raj", …}
Advertisements