JavaScript Set add() Method Tutorial

In this section, we will learn what the add() method is and how to use it in JavaScript.

What is JavaScript Set add() Method?

When we want to add a new element into a `Set` object, we use the `add()` method.

JavaScript Set add() Method Syntax:

add(value)

add() Function Parameters:

This method takes one argument and assigns that to the target `Set` object.

Note: if the argument is already in the collection, the assignment will be ignored.

add() Function Return Value:

The returned value of this method is a reference to the Set object with the new value added.

Example: using Set add() method in JavaScript

const collect = new Set();

collect.add("Jack");

collect.add("Jack");

collect.add("Omid");

collect.add("John");

Note: in the second statement in the program, because the value `Jack` is already in the collection, the second statement won’t add another value `Jack` to the collection.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies