JavaScript Set

To store the elements with unique primitive values or object references, a javascript set object is used.


new Set([iterable])

Where: iterable is the object whose elements will be added to the new Set.

Note: JavaScript set cannot contain duplicate elements i.e. it keeps unique elements only.

JavaScript Set Methods:

add() Use: To add the specified values to the Set object.

clear() Use: To remove all the elements from the Set object.

delete() Use: To delete the specified element from the Set object.

entries() To return an object of Set iterator that contains an array of [value, value] for each element.

forEach() Use: To execute the specified function once for each value.

has() Use: To indicate whether the Set object contains the specified value element.

values() Use: To return an object of the Set iterator that contains the values for each element.


<!DOCTYPE html>
const set1 = new Set([11, 22, 31, 45, 57]);
//output: false
//output: true
//output: false
Please follow and like us:
Content Protection by