- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Sets
A JavaScript Set object 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. The sets are introduced to JavaScript in ECMAScript 6 (ES6).
JavaScript Sets are similar to Arrays, but there are some key differences:
- Sets can only hold unique values, while Arrays can hold duplicate values.
- Sets are not ordered, while Arrays are ordered.
- Sets are faster to add and remove items from than Arrays.
JavaScript Sets are often used to store unique values, such as the unique users who have visited a website. They can also be used to remove duplicate values from an Array.
Syntax
Users can follow the syntax below to define a set in JavaScript −
let set1 = new Set([iterable]);
In the above syntax, we used the Set() constructor function with a ‘new’ keyword to define a set.
Parameters
- Iterable− It is an optional parameter. The Set() constructor function traverses through the elements of the iterable and creates a new set using those elements.
Examples
Example (Access set elements)
In the example below, we have passed the array containing the duplicate elements as an argument of the Set() constructor function. The num_set contains only unique values.
We used the values() method to get the iterator of the set values. To traverse the iterator, we use the for...of loop. In the loop, we access the element and print it. You can observe that set contains unique values even if the input array contains duplicate elements.
<html> <body> <p>The set elements are: </p> <p id = "output"></p> <script> const num_set = new Set([10, 20, 20, 20]); for (let value of num_set.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
The set elements are: 10 20
Example (Insert elements into the sets)
Users can use the add() method to insert the element into the set. Here, we have created the empty set. After that, we used the add() method 3 times to add the 60, 50, 50 elements into the set.
We inserted the 50 for 2 times, but the set contains only once as it contains unique values.
<html> <body> <p>Set elements after adding element/s to it: </p> <div id = "output"> </div> <script> const num_set = new Set(); num_set.add(60); num_set.add(50); num_set.add(50); for (let value of num_set.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
Set elements after adding element/s to it: 60 50
Example (Remove set elements)
The delete() method of the set allows you to delete the element from the set. Here, we created the set containing the various numbers and used the delete() method to delete the 200 and 400 from the set.
<html> <body> <p> After deleting elements from the set: </p> <div id = "output"> </div> <script> let num_set = new Set([100, 200, 300, 400, 500]); num_set.delete(200); num_set.delete(400); for (let value of num_set.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
The set contains 100?: true
Example (Check if the set contains a specific value)
The example below demonstrates of using the has() method to check whether the set contains the specific value.
Here, we check whether the set contains 100 and print the message in the output accordingly.
<html> <body> <p id = "output">The set contains 100?: </p> <script> const num_set = new Set([100, 200, 300, 400, 500]); document.getElementById("output").innerHTML += num_set.has(100); </script> </body> </html>
Output
It returns "true" because the element 100 is present in the set.
The set contains 100?: true
Mathematical set operations
The Set class doesn’t contain the built-in methods to perform the mathematical set operations like union, intersection, or set difference. So, you need to write the custom JavaScript code to perform the mathematical operations as shown below.
Example (Union of two sets)
The union of two sets contains all unique elements of set1 and set2.
In the example below, set1 and set2 contain the car names. We have defined the ‘union’ set and passed the array as an argument. We used the spread operator to create an array containing the elements of set1 and set2.
<html> <body> <p>The Union of set1 and set2 is: </p> <div id = "output"> </div> <script> const set1 = new Set(["BMW", "Audi", "TATA"]); const set2 = new Set(["BMW", "TaTa", "Honda", "Suzuki"]); const union = new Set([...set1, ...set2]); // Taking union for (let value of union.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
If we execute the program, it returns all unique elements of set1 and set2.
The Union of set1 and set2 is: BMW Audi TATA TaTa Honda Suzuki
Example (Intersection of two sets)
The intersection of two sets contains the unique elements which exist in set1 and set2 both.
Here, we have two sets containing the car names and defined the ‘inter’ set to store the set elements which exist in both sets.
We traverse through the elements of set1, and inside the loop we use the has() method to check whether the element of the set1 exists in the set2. If yes, we add an element into the ‘inter’ set.
<html> <body> <p> The intersection of set1 and set2 is: </p> <p id = "output"> </p> <script> const set1 = new Set(["BMW", "Audi", "TATA"]); const set2 = new Set(["BMW", "TATA", "Honda", "Suzuki"]); const inter = new Set(); for (let car of set1) { if (set2.has(car)) { inter.add(car); } } for (let value of inter.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
The intersection of set1 and set2 is: BMW TATA
Example (Difference of two sets)
The difference between set1 and set2 contains all elements in the set1 but not in the set2.
We have created the new set named ‘diff’ and initialized it with the ‘set1’ elements. After that, we traverse the set2 elements. If any element of the set2 exists in the ‘diff’ set, we delete it.
<html> <body> <p>The difference of set1 and set2 is: </p> <div id = "output"> </div> <script> const set1 = new Set(["BMW", "Audi", "TATA"]); const set2 = new Set(["BMW", "TATA", "Honda", "Suzuki"]); const diff = new Set(set1); for (let car of set2) { diff.delete(car); } for (let value of diff.values()) { document.getElementById("output").innerHTML += value + "<br> "; } </script> </body> </html>
Output
The difference of set1 and set2 is: Audi
JavaScript Set Reference
In JavaScript, a Set is a collection of unique values. In other words, each value can occur only once within a set. It provides methods to add, remove, and check for the presence of elements in the set. Here, we have listed the properties and methods of the Set class.
JavaScript Set Constructor()
Following is the constructor of Set in JavaScript −
Sr.No. | Name & Description |
---|---|
1 |
Creates and returns the set of unique values from values passed as an argument. |
JavaScript Set Properties
Following are the properties of Set class −
Sr.No. | Name & Description |
---|---|
1 |
This property returns the size of the set. |
JavaScript Set Methods
In the following table, we have listed all the properties of Set class −
Sr.No. | Name & Description |
---|---|
1 |
This method insert elements into the set. |
2 |
This method removes removes all elements of the set. |
3 |
This method deletes a single element of the set. |
4 |
This method returns elements in first set but not in the second set. |
5 |
To get an iterator containing all set entries. |
6 |
This method executes a provided function once for each value in this set. |
7 |
This method indicates whether an element with the specified value exists or not. |
8 |
This method returns the common elements in both sets. |
9 |
This method is an alias for values() method. |
10 |
This method returns a new Set iterator object that containing values for each element in a Set object. |