The values method of the JavaScript Set returns a new Set iterator object. This iterator object encompasses the values of each element within the Set. It preserves the order of insertion.
Syntax
The syntax for the values method is as follows:
Example
setObj.values()
Return
A new object of set iterator.
JavaScript Set values method example
In this section, we will explore the values method by examining multiple examples.
Example 1
Let's see a simple example of values method.
Example
<script>
var set = new Set();
set.add("jQuery");
set.add("AngularJS");
set.add("Bootstrap");
var itr=set.values();
document.writeln(itr.next().value+"<br>");
document.writeln(itr.next().value+"<br>");
document.writeln(itr.next().value);
</script>
Output:
Output
jQuery
AngularJS
Bootstrap
Example 2
Let's see the same example using for loop.
Example
<script>
var set = new Set();
set.add("jQuery");
set.add("AngularJS");
set.add("Bootstrap");
var itr=set.values();
for(i=0;i<set.size;i++)
{
document.writeln(itr.next().value+"<br>");
}
</script>
Output:
Output
jQuery
AngularJS
Bootstrap