Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Excellentpix Excellentpix

Intelligence Redefined

Excellentpix Excellentpix

Intelligence Redefined

  • Green Energy
  • Tech News
  • Gadget
  • Smartphone
  • Laptop
  • PC
  • About Us
    • Advertise Here
    • Contact Us
    • Privacy Policy
    • Sitemap
  • Green Energy
  • Tech News
  • Gadget
  • Smartphone
  • Laptop
  • PC
  • About Us
    • Advertise Here
    • Contact Us
    • Privacy Policy
    • Sitemap
Subscribe
Close

Search

How to use JavaScript set type, a beginner’s guide
Tech News

How to use JavaScript set type, a beginner’s guide

By Maria H. Gray
February 28, 2023 3 Min Read
0

JavaScript Sets are a new type of object (with ES6) that allows creating a collection of unique values. The values stored in a set can either be primitives like strings or integers or more complex types like arrays or objects. In this post, we will look into how to use Sets from a beginner’s point of view, let’s get started!

JavaScript Set a how to use it guide for beginners

Table of Contents #

What is a JavaScript Set #

As discussed earlier, JavaScript set is an object that only stores unique values both primitive and complex types. The uniqueness is compared with the === operator. JavaScript set is reported to be faster than array’s includes method possibly because the elements in a set are unique. Let’s look at a quick and useful example of removing duplicates from an array using sets.

Remove duplicates from an array using JavaScript set #

If there is a need to remove duplicates from an array, it can be done easily using Sets. We can deduplicate an array easily with the following ES6 code with spread operator:

const arrayWithDuplicateValues = [1, 2, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9];
const uniqueValues = [...new Set(arrayWithDuplicateValues)];

console.log(uniqueValues);

The same code can be done without the use of … spread operator as follows using Array.from:

const arrayWithDuplicateValues = [1, 2, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9];
const uniqueValues = Array.from(new Set(arrayWithDuplicateValues));

console.log(uniqueValues);

JavaScript Set can be used for more things too, like the one in the next section.

More usage of JavaScript Set #

Another interesting fact about JavaScript Set is that the items are ordered, meaning elements of
the set can be iterated in the insertion order. Below is another quick example:

let set01 = new Set('this is a very long sentence');
console.log(set01);
console.log(set01.size);
console.log(set01.has('b'));
console.log(set01.has('a'));

set01.add('b');
set01.has('b');
set01.delete('b');
set01.has('b');

As seen above a JavaScript Set has the size attribute that has the length of the Set. Similarly, to add an element the add method can be used and to check if a value is in the set the has method can be utilized. The delete method can be used to delete an existing element from the set. A JavaScript Set can be iterated through as well, below are some examples:

for (let letter of set01)  
console.log(letter);

Another way using values method on JavaScript Set is as follows:

const furnitureSet = new Set(['table', 'chair', 'sofa']);
const furnitures = furnitureSet.values();

console.log(furnitures.next());
console.log(furnitures.next());
console.log(furnitures.next());
console.log(furnitures.next().done);

The other way of showing these values without using the values method can be seen below:

const furnitureSet = new Set(['table', 'chair', 'sofa']);

furnitureSet.forEach(furniture =>
console.log('Furniture name: ', furniture);
);

That was pretty easy with a foreach loop that is available on the Set object. If you are interested in other basic set operations like union, intersection, and different please do read more about it.
Next, we will look into the compatibility of using JavaScript Set in the browser and Node.js.

Compatibility of JavaScript set #

JavaScript Set is available on both the browser and Node.js. In Node.js it is available from version 0.12 and above. For the browser, it is generally available on all modern browsers. It is safe to say JavaScript set can be used in both browsers and Node.js to get the maximum benefits.

Conclusion #

In this post, we took a quick look at how to use the JavaScript set from a beginner’s point of view. I hope it has helped you understand JavaScript (ES6) Set in a bit more detail and you will be able to use it for many things like getting unique records from a datastore after reading this. Keep learning!

Tags:

BeginnersguideJavaScriptSetType
Author

Maria H. Gray

Follow Me
Other Articles
Nest Rolls Out a Slew of New Holiday Ringtones for Your Doorbell – Review Geek
Previous

Nest Rolls Out a Slew of New Holiday Ringtones for Your Doorbell – Review Geek

9 Methods To Enhance The WEBSITE POSITIONING Of Each Web site You Design
Next

9 Methods To Enhance The WEBSITE POSITIONING Of Each Web site You Design

No Comment! Be the first one.

Leave a Reply Cancel reply

You must be logged in to post a comment.

September 2026
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  
« Aug    

Archives

Categories

  • Car
  • Entertainment
  • Gadget
  • Games
  • general
  • Green Energy
  • internet marketing
  • Laptop
  • Lifestyle
  • PC
  • Property
  • Real estate
  • SEO
  • Smartphone
  • Start Up
  • Tech News
  • Technology

Recent Posts

  • How a Review Management System Helps Businesses Build a Stronger Online Reputation
  • Rev Your Engines: Unleashing the Future of Smart and Sustainable Cars
  • The Untold Growth Hacks of Unicorn Startups: How Tiny Companies Become Billion-Dollar Giants
  • The Future Unveiled: Latest Tech Breakthroughs You Can’t Miss
  • Top Laptops of 2024: Unleashing Power, Portability, and Performance

fiver

Fiverr Logo

Tags

Amazon Android announces app Apple Big Black Buy Car coming deals Electric Galaxy game games gaming Google Heres Hunter iPhone laptop Laptops launch Market Microsoft news phone Pro Release Review sale Sales Samsung Series Smartphone software startup startups Stock tech Tesla top Windows Xbox year

PHP 2026

bussinessfair
codygut

BL

excellentpix.com

WhatsApp us