JavaScript Set add() Method (original) (raw)
Last Updated : 12 Jul, 2024
The **Set.add() method in JavaScript is used to append an element with a specified value in a set. It modifies the existing set by appending the new element but does not return a new set. The **Set.add() method does not modify the set if the element with a specified value already exists in the set.
**Syntax:
mySet.add(value);
**Parameters:
- ****value:**It is the value of the new element that is about to get appended to our set.
**Return value:
- It returns the set object with an added value
**Example 1: In this example, we have added two numbers 23 and 12 into the set.
JavaScript `
// Create a new set using Set() constructor let myset = new Set();
// Append new elements to the // set using add() method myset.add(23); myset.add(12);
// Print the modified set console.log(myset);
`
**Example 2: Adding elements using add() with chaining and checking if duplicate values exist or not.
JavaScript `
// Create a new set using Set() constructor let myset = new Set();
// Append new elements to the set // using add() method with chaining myset.add(23).add(12);
// Appending an already existing // element to the set this // element will not be added to the set // as it contains only distinct elements myset.add(23);
// Print the modified set console.log(myset);
`
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
Similar Reads
- JavaScript Set() Constructor The Javascript Set constructor is used to create Set objects. It will create a set of unique values of any type, whether primitive values or object preferences. It returns the new Set object. It mainly helps in creating a new set of objects which contains unique elements. Syntax:new Set()new Set(ite 3 min read
- JavaScript Set size Property The Set.size property in JavaScript returns the number of elements in a Set. In other words, we can say that this property is used to get the current size of the set. If the Set is empty the size of the set will be returned as 0. Syntax:mySet.sizeReturn Type:Number of elements in the setExample 1: I 2 min read
- JavaScript Set constructor Property JavaScript Set Constructor Property is used to return the Set constructor() for the object. It only returns the reference of the function and does not return the name of the function. In JavaScript set, it returns the function Set(){ [native code] }. Syntax Set.constructorParameter: It does not take 1 min read
- JavaScript Set add() Method The Set.add() method in JavaScript is used to append an element with a specified value in a set. It modifies the existing set by appending the new element but does not return a new set. The Set.add() method does not modify the set if the element with a specified value already exists in the set. Synt 3 min read
- JavaScript Set clear() Method The Set clear() method in JavaScript is used for the removal of all the elements from a set and making it empty. Syntax:mySet.clear();Parameters:This method does not accept any parameters.Return Value:Returns an undefined value.Example 1: Emptying set using Set clear() method [GFGTABS] JavaScript // 3 min read
- JavaScript Set delete() Method The delete() method in JavaScript Set deletes an element from the set and returns true if the element existed and was successfully deleted. Otherwise, it returns false if the element does not exist. Syntax:mySet.delete(value);Parameters:value: It is the value of the element that is to be deleted fro 3 min read
- JavaScript Set entries() Method The Set.entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in the order of their insertion. Although a Set does not contain key-value pairs, to keep similarities with the map.entries() method, it will return key-value pairs whe 3 min read
- JavaScript Set forEach() Method The set.forEach() method is used to execute the function which is taken as a parameter and applied for each value in the set, in insertion order. Syntax:forEach(function(value, key, set) { /* ... */ }, thisArg)Parameters:Callback function: The function will be executed for each value and it takes th 3 min read
- JavaScript Set has() Method The Set.has() method in JavaScript is used to check whether an element with a specified value exists in a Set or not. It returns a boolean value indicating the presence or absence of an element with a specified value in a Set. Syntax:mySet.has(value);Parameters:value: It is the value of the element 3 min read
- JavaScript Set values() Method The Set.values() method in JavaScript returns a new Iterator object that contains all of the items available in the set in a certain order. The order of values are in the same order that they were inserted into the set. Syntax:mySet.values()Parameters:This method does not accept any parameters.Retur 3 min read