Open In App

What is the use of the Values method in JavaScript Sets ?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript Sets, the values() method is used to return a new iterator object that contains the values for each element in the Set, in insertion order. The iterator object follows the iterable protocol, allowing you to loop through the values using methods like next().

Syntax:

mySet.values()
  • mySet: The Set for which you want to obtain an iterator for its values.

Example: Below is an example of a value method

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads