Convert A String To An Array Of Numbers In JavaScript

To convert a string to an array of numbers in JavaScript –

  1. Use the split() method to split the string to an array of strings.
  2. Use the map() method to iterate over the array and create a new array.
  3. In each iteration, convert the string to a number.

Let’s discuss this method in detail below.

convert a string to an array of numbers in JavaScript

Convert A String To An Array Of Numbers In JavaScript

We have an array of strings, and we want to convert it into an array of numbers.

To do this, we’ll use the split() method to split the string into an array of strings, then the map() method to iterate over the array and create a new array. In each iteration, we’ll use the Number() method to convert the string to a number.

Here’s an example:

let str = "1,2,3";

let arr = str.split(",");

let numArr = arr.map(Number);

console.log(numArr); //Output: [1, 2, 3]

In the above code, we first split the string into an array of strings using the split() method. Then, we iterated over the array using the map() method. In each iteration, we used the Number() method to convert the string to a number. Finally, we logged the new array numArr to the console.

The string above was separated by commas. It could have been separated by any other character, like a space.

For example:

let str = "1 2 3";

let arr = str.split(" ");

let numArr = arr.map(Number);

console.log(numArr); //Output: [1,2,3]

As you can see, this method is quite simple and straightforward. However, there is one issue with this method.

The Number() method can’t handle invalid strings, such as “1,2,3,hello”.

If you pass a string that cannot be converted to a number, the Number() method will return NaN.

Let’s see an example:

let str = "1,2,3,hello";

let arr = str.split(",");

let numArr = arr.map(Number);

console.log(numArr); //Output: [1, 2, 3, NaN]

In the above code, we tried to convert the string “1,2,3,hello” to an array of numbers. However, since the string “hello” cannot be converted to a number, the Number() method returned NaN.

If you want to include only numbers in the new array, you can use the filter() method to filter out the non-numeric values.

Here’s an example:

let str = "1,2,3,hello";

let arr = str.split(",");

let numArr = arr.map(Number).filter(Boolean);

console.log(numArr); //Output: [1,2,3]

In the above code, we first used the map() method to convert the string to an array of numbers. Then, we used the filter() method to filter out any falsy values. NaN is a falsy value in JavaScript.

But, there is an issue with this method as well.

This method will filter out 0 because 0 is also a falsy value in JavaScript.

To fix these issues, you can use the typeof operator to filter out any non-numeric values.

Let’s see an example:

let str = "1,2,3,hello";

let arr = str.split(",");

let numArr = arr.map(Number).filter(n => typeof n === 'number' && !isNaN(n));

console.log(numArr); //Output: [1, 2, 3]

As you can see, this method is a bit more complicated than the previous two methods. However, it is more reliable and will work in all cases.

Conclusion – Convert A String To An Array Of Numbers In JavaScript

In this article, we learned how to convert a string to an array of numbers in JavaScript.

There are many ways to do this, but the most reliable and straightforward method is to use the map() method with Number() and filter any invalid strings with typeof operator or isNaN method.

I hope you found this article helpful. Thanks for reading! 🙂

Leave a Reply