Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Set.prototype.add

Adds a value to a Set if it is not there yet.

Available since: ES2015

Syntax

set.add(value)

Parameters

ParameterTypeDescription
valueanyThe value to add.

Return value

SetThe same Set — calls can be chained.

Examples

Adding a value twice changes nothing:

const visited = new Set();
 
visited.add('home').add('about').add('home');
 
console.log(visited.size);
console.log([...visited].join(', '));

Output:

2
home, about

Notes

There is no error on a repeated add — the value just is not added a second time.

See also

In the tutorial

  • Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.

Thanks for reading!

Donate to the site — we work hard for you.