Horje
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.

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);  

Output
Set(2) { 23, 12 }

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);

Output
Set(2) { 23, 12 }

 Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

JavaScript Set add() Method- FAQs

What does the Set.prototype.add() method do in JavaScript?

The Set.prototype.add() method adds a new element to a Set object, if the element is not already present. It ensures that the set maintains unique elements.

How does the add() method handle duplicate values?

The add() method ignores duplicate values. If the value being added already exists in the Set, it will not be added again, and the Set remains unchanged.

Is the add() method chainable?

Yes, the add() method is chainable. You can call it multiple times in a single statement to add multiple values to a Set

Can the add() method be overridden?

Yes, you can override the add() method by defining a new add() method on a Set object instance or by modifying the Set.prototype.add.

What are some common use cases for the add() method?

Common use cases for the add() method include creating a collection of unique values, removing duplicates from an array, and efficiently managing a set of items where order and duplicates are not important.

We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Relation of Garbage Collector and Closure in JavaScript Relation of Garbage Collector and Closure in JavaScript
How to create Pay Role Management Webpage using HTML CSS JavaScript ? How to create Pay Role Management Webpage using HTML CSS JavaScript ?
Explain the symbol type in TypeScript Explain the symbol type in TypeScript
How can JavaScript codes be hidden from old browsers that do not support JavaScript ? How can JavaScript codes be hidden from old browsers that do not support JavaScript ?
What to understand Callback and Callback hell in JavaScript ? What to understand Callback and Callback hell in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8