In this section, we will learn what the Set values() method is and how to use it in JavaScript.
What is JavaScript Set values() Method?
We use the `values()` method to get an iterator object that contains all the values of a `Set` object.
Note: the iterator object is explained in later sections.
We can use this method and the for-of statement to loop through all the values of the target map.
JavaScript Set values() Method Syntax:
values()
values() Function Parameters:
The method does not take an argument.
values() Function Return Value:
The return value of this method is an iterator object that contains all the elements of the target Set object.
Example: using Set values() method in JavaScript
const collect = new Set(); collect.add("Jack"); collect.add("Jack"); collect.add("Omid"); collect.add("John"); collect.add(100); collect.add(200); for (const value of collect.values()){ console.log(value); }
Output:
Jack Omid John 100 200
Note: There’s another method named `keys()` that runs the same operation as the `values()` method does.
Example:
const collect = new Set(); collect.add("Jack"); collect.add("Jack"); collect.add("Omid"); collect.add("John"); collect.add(100); collect.add(200); for (const value of collect.keys()){ console.log(value); }
Output:
Jack Omid John 100 200