Open In App

JavaScript Set keys() Method

The set.keys() method is the same as the values() method. This means it returns the new iterator object that contains the value of each set in the insertion order. 

Syntax:



keys()

Parameter: This method does not take any parameters.

Return Type: It returns the new iterator object which holds the values in the set.



Example 1: In this example, we will see the use of keys in the set.




let placeSet = new Set();
  
placeSet.add("America");
placeSet.add("England");
placeSet.add("Chicago");
  
const setIterator = placeSet.keys();
  
console.log(setIterator);

Output:

[Set Iterator] { 'America', 'England', 'Chicago' }

Example 2: In this example, we will see the keys() method which returns the set iterator.




const set1 = new Set();
  
set1.add('0');
set1.add(1);
set1.add(1);
  
const iterator1 = set1.keys();
console.log(iterator1);

Output
[Set Iterator] { '0', 1 }

We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.

Supported Browser:

Article Tags :