
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ways to Create a Set in JavaScript
A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.
Ways to create a set in js−
1. Using empty Set constructor
let mySet = new Set(); mySet.add(1); mySet.add(1); console.log(mySet)
Output
Set { 1 }
2. Passing an iterable to the constructor
The set constructor accepts an iterable object(list, set, etc) using which it constructs a new set.
Example
let mySet = new Set([1, 2, 1, 3, "a"]); mySet.add(1); mySet.add(1); console.log(mySet)
Output
Set { 1, 2, 3, 'a' }
Advertisements