JavaScript Set add() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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
CallWhat 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.
set.sizeThe number of different values in the Set.

A standard JavaScript method (ES2015) on every Set, from Set.prototype. No import required. Siblings: clear(), delete(), difference(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Set.prototype.add(value)

// value  : any value (number, string, object, ...)
// returns: the Set itself

Parameters

ParameterDescription
valueRequired. 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 ValueDescription
SetThe 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.

JavaScript
const colors = new Set();
colors.add("red");
colors.add("green");
colors.add("red");

console.log("Size: " + colors.size);
console.log("Items: " + Array.from(colors).join(", "));
console.log("Has red: " + colors.has("red"));
Try It Yourself

How It Works

  1. Start empty. new Set() makes a Set with no values, so its size is 0.
  2. Add three times. "red" and "green" are new, so they are stored in that order. The second "red" is already there, so nothing changes.
  3. Check the result. The Set holds 2 values. Array.from() turns it into an array so join() can print it, and has() confirms that "red" is inside.

Example 2 — Chain add() Calls

See what add() returns, and use that to add several values in one line.

JavaScript
const numbers = new Set();
const result = numbers.add(1);

console.log("Returns the same Set: " + (result === numbers));

numbers.add(2).add(3).add(2).add(1);

console.log("Items: " + Array.from(numbers).join(", "));
console.log("Size: " + numbers.size);
Try It Yourself

How It Works

  1. Look at the return value. numbers.add(1) gives back numbers itself, so the comparison with === is true.
  2. Chain the calls. Because each call returns the Set, .add(2).add(3).add(2).add(1) works as one statement, from left to right.
  3. Duplicates vanish. The second 2 and the second 1 are ignored, so the Set ends with the 3 values 1, 2, 3 in the order they were first added.

Example 3 — See What Counts as a Duplicate

Add NaN, zeros, a number and a text, and objects, and check how many different values the Set keeps.

JavaScript
const items = new Set();
const shared = [];

items.add(NaN).add(NaN);
items.add(0).add(-0);
items.add("5").add(5);
items.add({}).add({});
items.add(shared).add(shared);

console.log("Size: " + items.size);
console.log("NaN stored: " + Array.from(items).filter(Number.isNaN).length);
console.log("Zero stored: " + Array.from(items).filter((x) => x === 0).length);
console.log("Shared array stored: " + Array.from(items).filter((x) => x === shared).length);
Try It Yourself

How It Works

  1. Special numbers. A Set treats NaN as equal to NaN (unlike ===), and 0 as equal to -0. So each pair adds only one value.
  2. 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.
  3. 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.

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.

ES2015 Standard since
Google Chrome 38+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium based
Full support
Mozilla Firefox 37+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Opera 25+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 38+ · Chromium based
Supported
Node.js 4+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer IE 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.

Next: clear()

Learn how to remove every value from a Set in one call, and why the Set object itself stays the same.

clear() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful