cft

You Complete Guide To Set Object In Javascript

Set is a collection of items and a built-in object, which stores only unique values. It has many methods like add(), entries(), values(), has(), delete(), clear()...


user

Aya Bouchiha

3 years ago | 1 min read

Hi everybody, I'm Aya Bouchiha, on this amazing day, we will talk about all Set Methods and Properties like has(), add(), and size.

Definition of Set Object

Set: is a collection of items and a built-in object, which stores only unique values.

Set Constructor

Iteration over a Set using For of loop

Size Property

size: is a set property that returns the length of the unique items in a specified set.

size
size

Set Methods

Set.prototype.add()

add(valueToAdd): is a set method that lets you add a new element to the specified Set.

add()
add()

Set.prototype.delete()

delete(valueToDelete): is a set method that lets you remove an element in a specified Set.

delete()
delete()

Set.prototype.clear()

clear(): means deleting all set elements.

clear()
clear()

Set.prototype.has()

has(valueToCheck): checks if the given value exists in a set.

has()
has()

Set.prototype.entries()

entries(): returns an array(iterator) [val, val] for each item in the specified Set.

entries()
entries()

Set.prototype.values()

values(): like keys(), It returns a new iterator object which contains the values for each element in the specified Set.

values()
values()

Set.prototype.forEach()

forEach(callback): this Set method invokes a callback for each element in the specified Set.

forEach()
forEach()

Summary

  • size: returns the length of the unique items in a specified set.
  • add(valueToAdd): lets you add a new element to the specified Set.
  • delete(valueToDelete):lets you remove an element in a specified Set.
  • clear(): delete all Set elements.
  • has(valueToCheck): checks if the given value exists in a set.
  • entries(): returns an array(iterator) [val, val] for each item in the specified Set.
  • values(): returns a new iterator object which contains the values for each element in the specified Set.
  1. forEach(callback): invokes a callback for each element in the specified Set.

References

Have a great day!

Upvote


user
Created by

Aya Bouchiha

Full-stack web developer who loves converting ideas to reality by creating amazing & creative websites, additionally, sharing information & good content with other people.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles