JavaScript – Add an Element to Set
To add an element to a Set in JavaScript, call add() method on this Set and pass the element as argument. If the element is not preset in the Set already, then the element is added to the Set, else, Set remains unchanged.
Syntax
The syntax to add an element e to the Set set1 is
set1.add(e)
ADVERTISEMENT
Examples
In the following example, we create an empty Set, and add elements to this Set using add() method.
index.html
If the element is already present in the Set, then the Set remains unchanged.
index.html
Conclusion
In this JavaScript Tutorial, we learned how to add an element to a Set in JavaScript using add() method, with examples.