Matheus Mello

Berlin, Germany

Convert Array of Numbers to Array of Strings in JavaScript

Cover Image for Convert Array of Numbers to Array of Strings in JavaScript
Matheus Mello
Matheus Mello

Convert Exhibit of Numbers to Cluster of Strings #

To switch a variety of numbers over completely to a variety of strings, call the guide() strategy on the exhibit, and on every emphasis, convert the number to a string. The guide technique will return another cluster containing just strings.

index.jsconst arrOfNum = [1, 2, 3]; const arrOfStr = arrOfNum.map(num => { return String(num); }); // 👇️ ['1', '2', '3'] console.log(arrOfStr);

The capability we passed to the Array.map technique gets called with every component (number) in the exhibit.

On every emphasis we convert the number to a string and bring it back.


The map method returns a new array that contains the values that the function
returned, in our case an array containing all the strings.

Note that the Array.map technique doesn't change the items in the first cluster, it returns another exhibit.

An elective methodology is to utilize the Array.forEach technique.

To switch a variety of strings over completely to a variety of numbers:

  • Pronounce a variable that will store the strings and set it to a vacant exhibit.

  • Utilize the forEach() technique to repeat over the numbers cluster.

  • On every emphasis convert the number to a string and push it to the strings exhibit.

index.jsconst arrOfNum = [1, 2, 3]; const arrOfStr = []; arrOfNum.forEach(num => { arrOfStr.push(String(num)); }); // 👇️ ['1', '2', '3'] console.log(arrOfStr);

We utilized the Array.forEach strategy to emphasize over the variety of numbers.

The capability we passed to the technique, gets called with every component in the exhibit.

On every emphasis we convert the number to a string and push it to the strings cluster.

Note that the Array.forEach strategy isn't upheld in Web Wayfarer. Assuming you need to help the program, utilize the Array.map approach all things considered.

Further Perusing #

  • Convert a Cluster to a String in JavaScript

  • Convert Cluster of Strings to Exhibit of Numbers in JavaScript


More Stories

Cover Image for Check if specific Text exists on the Page using JavaScript

Check if specific Text exists on the Page using Ja ...

Check if specific Text exists on the Page using JavaScript...

Matheus Mello
Matheus Mello
Cover Image for Check if String contains at least one Number in JavaScript

Check if String contains at least one Number in Ja ...

Check if String contains at least one Number in JavaScript...

Matheus Mello
Matheus Mello
Cover Image for Check if First Letter of String is Uppercase in JavaScript

Check if First Letter of String is Uppercase in Ja ...

Check if First Letter of String is Uppercase in JavaScript...

Matheus Mello
Matheus Mello