• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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 All Instances of Value From an Array in JavaScript
  • 2.  How to Use JavaScript to Round Up Numbers
  • 3.  Using JavaScript to Change Text of Element
  • 4.  Creating a JavaScript Function to Add Two Numbers
  • 5.  How to Call a JavaScript Function From HTML
  • 6.  Using JavaScript to Round to 2 Decimal Places
  • 7.  Using JavaScript to Check if Number is Divisible by Another Number
  • 8.  Using JavaScript to Set Select to the First Option
  • 9.  Using JavaScript to Redirect After 5 Seconds
  • 10.  Using JavaScript to Redirect to a Relative URL

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 *

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