Set.prototype.add
Appends a new element with a specified value to the end of a Set object
Syntax
set.add(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | T | The value of the element to add |
Return Value
The Set object (allows chaining)
Examples
const set = new Set<number>();
set.add(1).add(2).add(3);
console.log(set.size); // 3const tags = new Set<string>();
tags.add('javascript');
tags.add('typescript');
tags.add('javascript'); // duplicate, ignored
console.log(tags.size); // 2const unique = <T>(arr: T[]): T[] => [...new Set(arr)];
console.log(unique([1, 2, 2, 3, 3, 3])); // [1, 2, 3]Understanding Set.prototype.add
The Set.prototype.add method in JavaScript appends a new element with a specified value to the end of a Set object. It belongs to the Set object and is one of the most widely used methods for working with set values in modern JavaScript and TypeScript applications.
The method signature is set.add(value). It accepts 1 parameter: value. When called, it returns the set object (allows chaining). Understanding when and how to use add() helps you write more expressive, readable code.
Common use cases for Set.prototype.add include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like set-has, set-delete, set-clear, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Set.prototype.add is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Set Methods
Other methods in the Set object
Related Tools
More Set Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.