JavaScript Set add() method used to insert a new element to Set object with a specified value. Each element must have a unique value.
set.add(value)
JavaScript Set add
Simple example code understands add() method through various examples.
<!DOCTYPE html>
<html>
<body>
<script>
const set1 = new Set();
set1.add(1);
set1.add(1);
set1.add(2);
console.log(set1);
var set = new Set();
set.add("jQuery");
set.add("AngularJS");
set.add("Bootstrap");
for (let elements of set) {
console.log(elements)
}
</script>
</body>
</html>
Output:
add the elements to set objects in the chainable form.
const mySet = new Set();
mySet.add(1);
mySet.add(5).add('some text'); // chainable
console.log(mySet);
// Set [1, 5, "some text"]
Do comment if you have any doubts or suggestions on this Js set method topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version