ES6 Map & Set part 2
ES6 Set
A Set object can be used to hold unique values (no repetitions are allowed).
A value in a set can be anything (objects and primitive values).
The syntax new Set([iterable]) creates a Set object where iterable is an array or any other iterable object of values.
The size property returns the number of distinct values in a set.
For example:
Methods
add(value) Adds a new element with the given value to the Set.
delete(value) Deletes a specified value from the set.
has(value) Returns true if a specified value exists in the set and false otherwise.
clear() Clears the set.
values() Returns an Iterator of values in the set.
For example:
The above example demonstrates some of the ES6 Set methods.
Set supports different data types i.e. 1and "1" are two different values.
NaN and undefined can also be stored in Set.
Comments
Post a Comment