Skip to content

JavaScript set values() method

  • by

JavaScript set values() method returns a new Iterator object that contains the values for each element in the Set object in insertion order.

setObj.values()

Note: the values() method returns an iterator object, so we need to use the next() method to retrieve each value.

setIter.next().value

JavaScript set values() examples

Simple example code using the Set.prototype.values() method. The values() method on returns an iterator object containing the values in mySet. We then use the next() method on the iterator to retrieve each value in turn.

<!DOCTYPE html>
<html>
<body>
  <script >
    const mySet = new Set([1, 2, 3, 4, 5]);
    const iterator = mySet.values();

    console.log(iterator)
    console.log(typeof(iterator))

    console.log(iterator.next().value); // 1
    console.log(iterator.next().value); // 2
    console.log(iterator.next().value); // 3
    console.log(iterator.next().value); // 4
    console.log(iterator.next().value); // 5

  </script>
</body>
</html>

Output:

JavaScript set values() method

Get the values with iterator using the size property of Set

let i = 0;
while (i < myset.size) {
    console.log(setIterator.next().value);
    i++;
}

Do comment if you have any doubts or suggestions on the Js method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading