This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
10.9 JS Object Sets
A JavaScript Set is a collection of unique values.
Each value can only occur once in a Set.
A Set can hold any value of any data type.
Set Methods
Method
Description
new Set()
Creates a new Set
add()
Adds a new element to the Set
delete()
Removes an element from a Set
has()
Returns true if a value exists
clear()
Removes all elements from a Set
forEach()
Invokes a callback for each element
values()
Returns an Iterator with all the values in a Set
keys()
Same as values()
entries()
Returns an Iterator with the [value,value] pairs from a Set
Property
Description
size
Returns the number elements in a Set
How to Create a Set
You can create a JavaScript Set by:
Passing an Array to new Set()
Create a new Set and use add() to add values
Create a new Set and use add() to add variables
The new Set() Method
Pass an Array to the new Set() constructor:
// Create a Set
const letters = new Set(["a","b","c"]);
Create a Set and add literal values:
// Create a Set
const letters = new Set();
// Add Values to the Set
letters.add("a");
letters.add("b");
letters.add("c");
Create a Set and add variables:
// Create Variables
const a = "a";
const b = "b";
const c = "c";
// Create a Set
const letters = new Set();
// Add Variables to the Set
letters.add(a);
letters.add(b);
letters.add(c);
The add() Method
letters.add("d");
letters.add("e");
If you add equal elements, only the first will be saved:
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
If you add equal elements, only the first will be saved:
The forEach() Method
The forEach() method invokes a function for each Set element:
// Create a Set
const letters = new Set(["a","b","c"]);
// List all entries
let text = "";
letters.forEach (function(value) {
text += value;
})
The values() Method
The values() method returns an Iterator object containing all the values in a Set:
letters.values() // Returns [object Set Iterator]
Now you can use the Iterator object to access the elements:
// Create an Iterator
const myIterator = letters.values();
// List all Values
let text = "";
for (const entry of myIterator) {
text += entry;
}
The keys() Method
A Set has no keys.
keys() returns the same as values() .
This makes Sets compatible with Maps.
letters.keys() // Returns [object Set Iterator]
The entries() Method
A Set has no keys.
entries() returns [value,value] pairs instead of [key,value] pairs.
This makes Sets compatible with Maps.
// Create an Iterator
const myIterator = letters.entries();
// List all Entries
let text = "";
for (const entry of myIterator) {
text += entry;
}
Sets are Objects
For a Set, typeof returns object:
typeof letters; // Returns object
Try it yourself
For a Set, instanceof Set returns true:
letters instanceof Set; // Returns true
Try it yourself
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Variables - Objects are Variables part 1
JS Variables - Objects are Variables part 2
JS Objects - Using an Object Literal part 1
JS Objects - Using an Object Literal part 2
JS Objects - Using an Object Literal part 3
JS Objects - Using the Keyword new
JS Objects are Mutable
JS Object Properties - Access a property with .property
JS Object Properties - Access a property with ["property"]
JS Object Properties - Looping object property values
JS Object Properties - Add a new property to an existing object
JS Object Properties - Deleting object properties part 1
JS Object Properties - Deleting object properties part 2
JS Objects - Access nested objects part 1
JS Objects - Access nested objects part2
JS Objects - Access nested objects part 3
JS Objects - Access nested objects part 4
JS Arrays - Nested JS Objects and Arrays
JS Object Methods - this Keyword
JS Objects - Creating and using an object method part 1
JS Objects - Creating and using an object method part 2
JS Objects - Adding a Method to an Object
JS Objects - Using Built-In Methods
JS Objects - Displaying a JavaScript object
JS Objects - Display object properties
JS Objects - Displaying the Object in a Loop
JS Objects - Using Object.values()
JS Objects - Using JSON.stringify()
JS Objects - Stringify Dates
JS Objects - Stringify Functions part 1
JS Objects - Stringify Functions part 2
JS Arrays - Stringify Arrays
JS Getters and Setters - The get Keyword
JS Getters and Setters - The set Keyword
JS Object Methods part 1
JS Object Methods part 2
JS Getters and Setters - Data Quality part 1
JS Getters and Setters - Data Quality part 2
JS Getters and Setters - Object.defineProperty()
JS Object Constructors
JS Object Constructors - Add a Property to an Object
JS Object Constructors - Adding a Method to an Object
JS Object Constructors - Adding a Property to a Constructor part 1
JS Object Constructors - Adding a Property to a Constructor part 2
JS Object Constructors - Adding a Property to a Constructor part 3
JS Object Constructors - Adding a Method to a Constructor
Did You Know?
JS Objects - Using an object constructor
JS Objects - Using the prototype Property part 1
JS Objects - Using the prototype Property part 2
JS Iterables - Iterate over a String
JS Iterables - Iterating Over an Array
JS Iterables - Home Made Iterable part 1
JS Iterables - Home Made Iterable part 2
JS Sets - The new Set() Method part 1
JS Sets - The new Set() Method part 2
JS Sets - The new Set() Method part 3
JS Sets - The add() Method part 1
JS Sets - The add() Method part 2
JS Sets - The forEach() Method
JS Sets - The values() Method part 1
JS Sets - The values() Method part 2
JS Sets - The keys() Method
JS Sets - The entries() Method
JS Map Objects - new Map()
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects - Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.clear()
JS Maps using Map.has()
JS Map Objects - Map typeof
JS Map Objects - Map instanceof
JS Map Objects - Using Map.forEach()
JS Map Objects - Using Map.entries()
JS Map Objects - Using Map.keys()
JS Map Objects - Using Map.values() part 1
JS Map Objects - Using Map.values() part 2
JS Map Objects - Objects as Keys part 1
JS Map Objects - Objects as Keys part 2
JS Object - Changing a Property Value
JS Objects - Listing All Properties
JS Objects - Listing Enumerable Properties
JS Objects - Adding a Property
JS Objects - Adding Getters and Setters
JS Objects - A Counter Example