O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.1007/978-1-4302-6107-0_10

10. Working with Sets

Russ Ferguson and Keith Cirkel2
(1)
Ocean, New Jersey, USA
(2)
London, UK
 

What Is the Difference Between a Set and an Array ?

Problem

In what situation would you use a set object over an array?

Solution

There are some similarities between the two objects. They can both hold on to data of different types. What separates a set is that the values all need to be unique.

The Code

var numberSet = new Set();
    numberSet.add(1);
    numberSet.add(2);
    numberSet.add(3);
    numberSet.add(3); //does not get added
    console.log(numberSet.entries()); //returns SetIterator {[1, 1], [2, 2], [3, 3]}
var numArray = new Array();
    numArray.push(1); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required