Suppose you need help with how to create a shallow copy of a set in JavaScript. Don’t worry too much because we have already tested effectively using the Set() constructor to create a shallow copy of Set. Let’s learn with us to know how it works.
What is the shallow copy?
To know how to create a shallow copy of a set in JavaScript, the first thing you must know is what a shallow copy is.
Understand simply, a shallow copy means that the new variable or its components are still related to the original variable after copying. For shallow copies, it’s essential to understand that selectively changing the value of a shared property of an existing element in an object is different from assigning a completely new value to an existing element.
Let us take an example to understand it better about the shallow copy.
Example:
In the example below, even though a is defined as const, we can still change the value. Do you know why? Back in the day, we actually made a shallow copy in the example above. This will often cause errors if we use it inappropriately. Instead of changing the value of the new variable, we also change the value of the original variable.
const player = { name: "Cristiano Ronaldo", age: 37, nationality: "Portugal", salary: "26,8 million GBP" }; console.log("Player name => ", player.name); var newPlayer = player; // Shallow copy console.log("New Player name => ", newPlayer.name);
Output
Player name => Cristiano Ronaldo
New Player name => Cristiano Ronaldo
Create a shallow copy of a Set in JavaScript
Using the Set()
constructor
To create a shallow copy of a set in JavaScript, we just use the constructor to clone the Set. Just only like this:
var clonedSet = new Set(originalSet);
The Set constructor lets you create Set objects that store unique values of any type, whether primitive values or object references.
To compare two sets after create a shallow copy, we already have the article to show some ways to compare two Sets here.
Syntax:
New Set()
New Set(iter)
Parameter:
- iter: all elements of iter will be added to the new element if this parameter is non-empty.
Return value: A new Set object.
Code example:
Let’s see the code example below.
const mySet = new Set(["Croatia", "Argentina", "Morocco", "France"]); console.log("My Set =>", mySet); // Create a shallow copy of mySet const newSet = new Set(mySet); console.log("New Set =>", newSet); // Two Set is the same value but not equals console.log("Comparing Set and newSet: ", mySet === newSet); // false
Output
My Set => Set(4) {size: 4, Croatia, Argentina, Morocco, France}
New Set => Set(4) {size: 4, Croatia, Argentina, Morocco, France}
Comparing Set and newSet: false
Browser Support
The Set constructor
is an ECMAScript1 (ES1) feature.
This feature is supported in all browsers, and you can use it for all browsers. Very common function in JavaScript.
Summary
Hopefully, through this article, you can easily understand how to create a shallow copy of a Set in JavaScript. Using the Set constructor
is the way we have shown in this article, but how about you? Leave your comment here if you have any questions about this article. Thank you for your reading!
Maybe you are interested:
- How to check if two Sets are equal using JavaScript
- How To Merge Sets In JavaScript
- How to get the Union of Two Sets in JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python