Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Sets

JS Sets

A JavaScript Set is a collection of unique values. Each value can only occur once in a Set.

The Set Methods

The JS Set has the following methods and properties:

  • new Set(); create new set
  • add(); adds a new element to the Set
  • delete(); removes an element from a Set
  • has(); returns true if a value exists in the Set
  • forEach(); invokes a callback for each element in the Set
  • values(); returns an iterator with all the values in a Set
  • size; property that returns the number of elements in a Set
The new Set() Method

The JS new Set() method is used to create a JS Set. You can pass variable, values, array, objects etc.

					 
        
          let num = 1;
          const alphabets = ['x', 'y', 'z'];

          const set1 = new Set();
          const set2 = new Set('a');
          const set3 = new Set(num);
          const set3 = new Set(['a', 'b', 'c']);
          const set4 = new Set(alphabets);
        
      
The add() Method

The JS add() method is used to add values to the Set. If you add equal elements, only the first will be saved.

					 
        
          const mySet = new Set();

          mySet.add('a');
          mySet.add('b');
          mySet.add('c');
          mySet.add('d');
          mySet.add('a');
          mySet.add('b');
          mySet.add('a');
        
      
The forEach() Method

The JS forEach() method invokes (calls) a function for each Set element.

					 
        
          const mySet = new Set(["a","b","c"]);

          // List all Elements
          let text = "";
          mySet.forEach (function(value) {
            text += value;
          });
        
      
The values() Method

The JS values() method returns a new iterator object containing all the values in a Set so you can use the Iterator object to access the elements.

					 
        
          const mySet = new Set(["a","b","c"]);

          mySet.values() // JS Set Iterator having Set values

          let text = "";
          for (const x of mySet.values()) {
            text += x;
          }
        
      
The has() Method

The JS has() method returns true if the Set has the passed element.

					 
        
          const mySet = new Set(["a","b","c"]);
          mySet.has('a'); // true
          mySet.has('h'); // false
        
      
The delete() Method

The JS delete() method deletes the passed element from the Set.

					 
        
          const mySet = new Set(["a","b","c"]);
          mySet.delete('a'); // ['b', 'c']
        
      
The size Property

The JS size property return the size of the Set.

					 
        
          const mySet = new Set(["a","b","c"]);
          mySet.sizs; // 3
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved