DEV Community

Cover image for JavaScript Set
Bello Osagie
Bello Osagie

Posted on β€’ Edited on

1

JavaScript Set

A Set is a collection of a set of values without keys.

The common methods and properties are shown below:

  • new Set(iterable) – creates the set, and if an iterable object (eg. array)
  • set.add(value) – adds a value
  • set.delete(value) – removes the value
  • set.has(value) – returns true if the value exists, otherwise false.
  • set.clear() – removes everything from the set.
  • set.size – returns the current element count.

Set calls

Repeated calls of the same set.add(value) don't count.

See the example below:

const set = new Set();

const bello = { name: 'Bello' };
const monica = { name: 'Monica' };
const jerry = { name: 'Jerry' };

set.add(bello);
set.add(monica);
set.add(monica);
set.add(jerry);
set.add(jerry);

console.log( set.size ); // 3

for (let user of set) {
  console.log(user.name);

/*
3
Bello
Monica
Jerry
*/
}
Enter fullscreen mode Exit fullscreen mode

The example above shows that Set prevents repetition of properties in an iterable object.

Let's filter out all repeating items in the array or prevent items from repeating more than once in the array.

See the example below:

const randomWords = [ 
  'pursue', 'admire', 'power', 'pursue', 
  'advocate', 'begin', 'admire', 'love', 'joyful' 
];

const uniqueRandomWords = [ ...new Set(randomWords) ];
console.log(uniqueRandomWords);

/*
[
  'pursue', 'admire',
  'power',  'advocate',
  'begin',  'love',
  'joyful'
]
*/
Enter fullscreen mode Exit fullscreen mode

Iteration over Map

There are 3 methods to loop over a set β€” the same methods are used on map.

  • set.keys() - returns an iterable for keys,
  • set.values() - returns an iterable for values,
  • set.entries() - returns an iterable for entries

See the example below:

const set = new Set([ 'Apple', 'Orange', 'Melon']);

// for...of
for (let value of set) { 
  console.log(value);

/*
Apple
Orange
Melon
*/

}

// forEach:
set.forEach((value, valueAgain, set) => {
  console.log(value);

/*
Apple
Orange
Melon
*/

});
Enter fullscreen mode Exit fullscreen mode

value and valueAgain are the same value for compatibility with Map

Happy coding!!!


Buy me a Coffee


SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay