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