The add() method in JavaScript’s Set object is use to add a new element to the Set. If the element already present, it is not added again in the set, ensuring all elements presents in the set are unique. This method return a set object.
Syntax
add() method represnts by the following syntax
Var mySet = new Set() mySet.add(value)
Value – It reprents that new element added in the set.
Return
It return set object with added new value.
Example of Set add() Method
// Create a new Set let mySet = new Set(); // Add values to the Set mySet.add(11); mySet.add(22); mySet.add(11); // Duplicate values are ignored mySet.add("CodeSpeedy"); // Log the Set to the console console.log(mySet)
Output
Set(3) { 11, 22, 'CodeSpeedy' }
Conclusion
In short, the add() method of the JavaScript Set object is a simple and efficient way to manage collections of unique values. This ensures that no duplication is added, making your data management more efficient.