Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Set values() Method

JavaScript Set values() Method

The Set.values() method in JavaScript returns a new Iterator object that contains all of the items available in the set in a certain order. The order of values are in the same order that they were inserted into the set.

Syntax:

mySet.values()

Parameters: This method does not accept any parameters.

Return Value: The Set.values() method returns a new iterator object which holds the values for each element present in the set.

The below examples illustrate the Set.values() method:

Example 1:

Javascript




let myset = new Set();
 
// Adding new element to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");
 
// Creating a iterator object
const setIterator = myset.values();
 
// Getting values with iterator
console.log(setIterator.next().value);
console.log(setIterator.next().value);
console.log(setIterator.next().value);


Output:

California
Seattle
Chicago

Example 2:

Javascript




let myset = new Set();
 
// Adding new element to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");
 
// Creating a iterator object
const setIterator = myset.values();
 
// Getting values with iterator using
// the size property of Set
let i = 0;
while (i < myset.size) {
    console.log(setIterator.next().value);
    i++;
}


Output:

California
Seattle
Chicago

Supported Browsers:

  • Chrome 38 and above
  • Edge 12 and above
  • Firefox 24 and above
  • Opera 25 and above
  • Safari 8 and above

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript. 

RELATED ARTICLES

Most Popular

Recent Comments