DEV Community

Cover image for The Set() Object in JavaScript - A Nimble Introduction
RakibRahman
RakibRahman

Posted on

The Set() Object in JavaScript - A Nimble Introduction

Hello, I am going to give you a quick introduction about JavaScript Set() object and its must know properties and methods.

The Set() Object

The Set object is a collection of elements. It can store primitive values or object references.Set can only contains distinctive elements,so With Set object you can remove duplicate elements.


Using the Set object

  • Setting up an empty Set object empty set
  • Set object With array set with array

Iterating Sets

  • You can iterate over items in Set object using for...of statement. Iterating Sets

Adding an element to a set

  • You can add an element to the end of a Set object via the add() method. Alt Text

Number of elements in a Set

  • To see the number of (unique) elements in a Set object use the size property. size

Checking an element in the set

  • The has method will tell you whether a value exists in a Set object. has

Delete elements from set

  • Using delete method you can remove a value from the Set object. delete

Remove all elements from set

  • To remove all the elements from Set object simply use clear method. clear

Hope you have learnt something from this article.Kindly forgive any mistake as it is my first article. Happy Coding!

Top comments (0)