Convert An Array Of Strings To Array Of Numbers In JavaScript

To convert an array of strings to array of numbers in JavaScript, you can use any of these methods –

  1. Use the map() method to iterate over the array and convert each element to a number using the Number() method.
  2. Use the forEach() method to iterate over the array and convert each element to a number using the Number() method.
  3. Use Array.from(array, func) to iterate over the array and convert each element to a number using the Number() method.

Let’s discuss each of these methods in detail below.

convert an array of strings to array of numbers in javascript

Use map() Method To Convert Array Of Strings To Array Of Numbers

The map() method is the simplest and most elegant way to convert an array of strings to array of numbers in JavaScript. It is also the most efficient way as it doesn’t create a new array, but just modifies the existing one.

Here’s how you can use the map() method to convert an array of strings to array of numbers.

var arr = ["1", "2", "3"];

var numbers = arr.map(Number);

console.log(numbers); // output: 1,2,3

As you can see from the above code, all we did was pass the Number() function as the callback to the map() method. This caused each element of the array to be passed to the Number() function which converts it to a number and returns the result.

Use forEach() Method To Convert Array Of Strings To Array Of Numbers

If you don’t want to use the map() method, then you can also use the forEach() method to convert an array of strings to array of numbers in JavaScript.

var arr = ["1", "2", "3"];

var numbers = [];

arr.forEach(function(element) {

   numbers.push(Number(element));

});

console.log(numbers); // output: 1,2,3

As you can see from the above code, we first created an empty array. We then iterated over the input array using the forEach() method. For each element of the array, we converted it to a number using the Number() function and added it to the numbers array.

Use Array.from(array, func) To Convert Array Of Strings To Array Of Numbers

If you want to use the functional programming style, then you can use the Array.from() method to convert an array of strings to array of numbers in JavaScript.

var arr = ["1", "2", "3"];

var numbers = Array.from(arr, Number);

console.log(numbers); // output: 1,2,3

As you can see from the code above, we first created an array from the input array using the Array.from() method. We then passed the Number() function as the second argument to the Array.from() method which caused each element of the input array to be passed to the Number() function and converted to a number.

Note that the Array.from() method is not supported in IE 11 and below.

Conclusion

In this article, you saw three different ways to convert an array of strings to array of numbers in JavaScript. The map() method is the simplest and most elegant way to do it. The forEach() method is also pretty simple, but it’s not as efficient as the map() method. If you want to use the functional programming style, then you can use the Array.from() method to convert an array of strings to array of numbers in JavaScript.

I hope you found this article helpful. If you have any questions, please feel free to post them in the comments section below. Thanks for reading! 🙂

Leave a Reply