set.add(value) puts a value into a Set. If the value is not in the Set yet, it is added at the end. If it is already there, nothing happens, because a Set never keeps the same value twice.
The method returns the Set itself, not true or the new size. That is why you can chain calls like set.add(1).add(2).add(3).
Remember
set.add(value)
new value → added at the end
already there → nothing changes
returns → the same Set (so you can chain)
Duplicates are found with "same value":
NaN equals NaN, 0 equals -0, "5" is not 5
Syntax
Call it on a Set and pass the value to store. The result can be ignored or chained.
JavaScript
const colors = new Set();
colors.add("red");
colors.add("green").add("blue"); // chaining
colors.add("red"); // ignored: already there
console.log(colors.size); // 3
Call
What it does
set.add(value)
Add value to the end of the Set, unless it is already there.
set.add(a).add(b)
Add two values in one line, because add() returns the Set.
new Set([1, 2, 2, 3])
Fill a Set from an array when you create it. Duplicates are dropped.
// ECMAScript 2015 (ES6)
Set.prototype.add(value)
// value : any value (number, string, object, ...)
// returns: the Set itself
Parameters
Parameter
Description
value
Required. The value to store. It can be any type. Two values count as the same when they are equal by "same value zero": NaN equals NaN, 0 equals -0, and objects are the same only when they are the very same object.
Return Value
Return Value
Description
Set
The same Set that you called add() on, so you can chain more calls. It does not tell you whether the value was new. Compare size before and after, or use has(), if you need to know.
JavaScript Examples
Example 1 — Add Values to a Set
Create an empty Set, add three values (one of them twice), and look at the result.
Size: 7
NaN stored: 1
Zero stored: 1
Shared array stored: 1
How It Works
Special numbers. A Set treats NaN as equal to NaN (unlike ===), and 0 as equal to -0. So each pair adds only one value.
Types matter. The text "5" and the number 5 are different values, so both are stored. The two empty objects {} are different objects, so both are stored too.
Same object, one entry.shared is added twice but is one array, so it is stored once. In total: NaN, 0, "5", 5, two objects and shared make 7 values.
Compatibility
Browser & Runtime Support
Set.prototype.add() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set.add()
Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer 11 only has a basic Set.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · Chromium based
Supported
Node.js4+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerIE 11 · basic Set only
Partial support
set.add()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.
Frequently Asked Questions
It adds a value to the end of a Set. If the value is already in the Set, nothing changes. It returns the Set itself, so you can chain calls such as set.add(1).add(2).
No. Unlike delete(), which returns true or false, add() returns the Set itself. If you need to know whether the value was new, compare set.size before and after the call, or call set.has(value) first.
A Set compares objects by identity, not by content. Two different objects with the same properties are two different values. Add the very same object (the same variable) twice and only one copy is kept.
Yes. A Set uses "same value zero" to compare values, so NaN is equal to NaN and -0 is equal to 0. That means a Set stores only one NaN, and a stored -0 is kept as 0.
Pass an array or any other iterable when you create it: new Set([1, 2, 3]). To add to an existing Set, loop with for...of and call add() each time, or chain several add() calls.