# Convert all Values in a Set to Uppercase in JavaScript

Borislav Hadzhiev

Mon Nov 01 20212 min read

## Convert all Values in a Set to Uppercase#

To convert all values in a Set to uppercase, create a second `Set`, that will store the uppercase values. Use the `foreach()` method to iterate over the first `Set` and on each iteration, convert the value to uppercase and add it to the second `Set`.

index.js
```Copied!```const set1 = new Set(['one', 'two', 'three']);

const set2 = new Set();

set1.forEach(value => {
set2.add(value.toUpperCase());
});

console.log(set2); // ๐๏ธ {'ONE', 'TWO', 'THREE'}
``````

We created a second Set that will store the uppercase values.

The function we passed to the Set.forEach method gets called with each element in the `Set`.

On each iteration, we convert the value to uppercase and add it to the new `Set`.

The Set.add method takes a value as a parameter and adds it to the `Set` object.

After the last iteration, the second `Set` contains the uppercase equivalent of the values in the first `Set`.

An alternative, but more functional approach is to use the Array.map method.

To convert all values in a Set to uppercase:

1. Convert the `Set` to an array and call the `map()` method on the array.
2. On each iteration, convert the value to uppercase.
3. Convert the array back to a `Set`.
index.js
```Copied!```const set1 = new Set(['one', 'two', 'three']);

const set2 = new Set(
Array.from(set1).map(value => value.toUpperCase())
);

console.log(set2); // ๐๏ธ {'ONE', 'TWO', 'THREE'}
``````

The first step is to convert the `Set` to an array, so we can call the `map()` method on it.

The function we passed to the `map` method gets called with each element in the array.

Whatever we return from the callback function gets added to the array that the `map` method returns.

On each iteration, we uppercase the array element and return the result.

The last step is to convert the array back to a `Set` object.

For `Set` objects that have hundreds of thousands of elements, this approach might be slower than the first, because of the back and forth conversions.

It's a matter of personal preference which approach you pick. I prefer the first one, because it's quite easy to read and does not require any conversions.

## Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Copyright ยฉ 2021 Borislav Hadzhiev