
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Ways to create a dictionary of lists in python
- Various ways to define a variable in JavaScript
- Different ways to create Objects in Java
- Ways to get to a specific sum in JavaScript
- Python - Ways to create a dictionary of Lists
- Bell Numbers - Number of ways to Partition a Set in C++
- 3 Ways to Set a Static IP Address in RHEL 8
- 5 different ways to create objects in Java
- Different ways to create an object in java?
- 3 Ways to Create a Network Bridge in RHEL CentOS 8
- Ways to Create an ISO from a Bootable USB in Linux
- Count number of ways to partition a set into k subsets in C++
- Finding power set for a set in JavaScript Power Set
- How to set a countdown timer in javascript?
- Python - Ways to create triplets from given list

Advertisements