• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Add Items to Set

Using JavaScript to Add Items to Set

September 15, 2022 Leave a Comment

In JavaScript, to add to a set, you can use the add() method. add() will add an element to the set if the element is not already in the set.

var numbersSet = new Set([1, 2, 3]);

numbersSet.add(4);

console.log(numbersSet);

#Output:
{1, 2, 3, 4}

You can also use the Spread Operator (…) to add multiple elements from an array to a set.

var numbersSet = new Set([1, 2, 3]);

var arr1 = [4,5];
var combinedSet1 = new Set([ ...numbersSet, ...arr1 ]);

console.log(combinedSet1);

#Output:
{1, 2, 3, 4, 5}

var set2 = new Set([6,7]);
var combinedSet2 = new Set([ ...combinedSet1, ...set2 ]);

console.log(combinedSet2);

#Output:
{1, 2, 3, 4, 5, 6, 7}

When working with collections of data in JavaScript, the ability to easily add items or change the collection is important.

One such case where you may want to modify a collection is if you want to add elements to a set in JavaScript.

To add on item to a set in JavaScript, you can use the add() method. add() will add an element to the set if the element is not already in the set.

Below is our example again of how you can use add() method to add one item to a set in JavaScript.

var numbersSet = new Set([1, 2, 3]);

numbersSet.add(4);

console.log(numbersSet);

#Output:
{1, 2, 3, 4}

Adding Multiple Items to Set in JavaScript with the Spread Operator (…)

If you want to add multiple items to a set in JavaScript, then you could use the add() method multiple times, or use the Spread Operator (…).

Below shows you how to add multiple elements to a set using Spread Operator (…) in JavaScript.

var numbersSet = new Set([1, 2, 3]);

var arr1 = [4,5];
var combinedSet1 = new Set([ ...numbersSet, ...arr1 ]);

console.log(combinedSet1);

var set2 = new Set([6,7]);
var combinedSet2 = new Set([ ...combinedSet1, ...set2 ]);

console.log(combinedSet2);

#Output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6, 7}

Hopefully this article has been useful for you to learn how to use JavaScript to add items to a set.

Other Articles You'll Also Like:

  • 1.  Remove Parentheses From String Using JavaScript
  • 2.  How to Create a JavaScript Countdown Timer
  • 3.  Using JavaScript to get Textarea Value
  • 4.  Using JavaScript to Check if Array is Empty
  • 5.  JavaScript Round to Nearest 10
  • 6.  Remove Special Characters From String in JavaScript
  • 7.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 8.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 9.  Create Array of Zeros in JavaScript
  • 10.  Using JavaScript to Check if Variable is a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy