Open In App

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

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()

Example: Below is an example of a value method




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
Article Tags :