My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Post hidden from Hashnode

Posts can be hidden from Hashnode network for various reasons. Contact the moderators for more details.

The Set() Object in JavaScript - A Nimble Introduction

The Set() Object in JavaScript - A Nimble Introduction

Rakib Talukder's photo
Rakib Talukder
·Mar 1, 2021·

2 min read

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!