The Set add() Method
If you add equal elements, only the first will be saved:
Example
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
Try it Yourself »
Description
The add()
method inserts a new element into a set.
Note
The primary feature of Set objects is that they only store unique values.
If an attempt is made to add an element that already exists in the set, the add()
method will have no effect,
and the set will remain unchanged.
Syntax
set.add(value)
Parameters
Parameter | Description |
value | Required. The value to add. |
Return Value
Type | Description |
Set | The original set object with the added value. |
The add()
method directly modifies the existing Set object.
It does not return a new Set object.
Related Set Methods:
See Also:
Browser Support
set.add()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
set.add()
is not supported in Internet Explorer.