Basics
JS Sets
Working with JavaScript Sets
JavaScript Sets store unique values, with methods like add and has.
Introduction to JavaScript Sets
JavaScript Sets are a part of ES6 and offer a way to store unique values of any type, whether primitive values or object references. They are similar to arrays but come with additional benefits when dealing with unique data.
Creating a Set
You can create a Set using the Set
constructor. Initially, you may initialize it as empty or with an iterable object.
Adding Values to a Set
To add values to a Set, use the add()
method. Remember, a Set automatically ignores duplicate values.
Checking for Values
Use the has()
method to check if a value exists in a Set. It returns true
if the value is present, and false
otherwise.
Removing Values from a Set
To remove an item from a Set, use the delete()
method. The method returns true
if the item was successfully removed, and false
if the item was not found.
Iterating Over a Set
Sets are iterable, meaning you can loop through the elements using various methods, such as forEach()
or a for...of
loop.
Conclusion
JavaScript Sets provide a useful way to store unique values and perform operations like adding, checking, and removing elements efficiently. Understanding Sets is an essential skill for managing collections of data effectively in JavaScript.
Basics
- Introduction
- Where To
- Output
- Syntax
- Comments
- Variables
- Scope
- Hoisting
- Errors
- Data Types
- Operators
- Ternary Operator
- Short-Circuit Evaluation
- If Else
- Switch
- Loops
- For...Of/For...In
- Functions
- this Keyword
- Objects
- Arrays
- Strings
- Template Literals
- Numbers
- Number Properties
- Dates
- Math
- Booleans
- Type Conversion
- Destructuring
- Spread/Rest
- RegExp
- Strict Mode
- Modules
- Security Basics
- Debugging
- Best Practices
- Mistakes
- Performance
- Reserved Words
- Sets
- Maps
- Bitwise
- Array Const
- Previous
- Reserved Words
- Next
- Maps