

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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' }
- Related Questions & Answers
- Python - Ways to create a dictionary of Lists
- Various ways to define a variable in JavaScript
- Different ways to create Objects in Java
- Ways to get to a specific sum in JavaScript
- Bell Numbers - Number of ways to Partition a Set in C++
- 5 different ways to create objects in Java
- Different ways to create an object in java?
- Different ways to change a character into uppercase in javascript?
- Python - Ways to create triplets from given list
- Count number of ways to partition a set into k subsets in C++
- Finding power set for a set in JavaScript Power Set
- No. of ways to empty an array in JavaScript
- What are the different ways to create an object in Java
- How to set a countdown timer in javascript?
- In how many ways can we split a string in JavaScript?
Advertisements