JavaScript Set values() Method (original) (raw)

Last Updated : 12 Jul, 2024

The **Set.values() method in JavaScript returns a new Iterator object that contains all of the items available in the set in a certain order. The order of values are in the same order that they were inserted into the set.

**Syntax:

mySet.values()

**Parameters:

**Return Value:

The below examples illustrate the **Set.values() method:

**Example 1:

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

**Example 2:

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 using // the size property of Set let i = 0; while (i < myset.size) { console.log(setIterator.next().value); i++; }

`

**Output:

California
Seattle
Chicago

**Supported Browsers:

Similar Reads