JavaScript Set values() Method
Last Updated : 12 Jul, 2024
Improve
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:
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:
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
- Edge
- Firefox
- Opera
- Safari