Skip to content
the poor coder

convert an array of strings to numbers

Poor Man
Published
Reading time
2 min read
On this page

How to Convert an Array of Strings to Numbers#

Converting an array of strings to numbers is a common task in programming. Fortunately, it is a straightforward process in most programming languages. In this post, I will explain how to do it in JavaScript.

Method 1: Using a For Loop#

One way to convert an array of strings to numbers is by using a for loop. Here is the code:

Text

const stringArray = ['1', '2', '3', '4', '5'];
const numberArray = [];

for (let i = 0; i < stringArray.length; i++) {
  numberArray.push(parseInt(stringArray[i]));
}

console.log(numberArray); // [1, 2, 3, 4, 5]

In this code, we first create an array of strings called stringArray. We also create an empty array called numberArray that we will use to store the converted numbers.

Next, we use a for loop to iterate over each element in stringArray. For each element, we use the parseInt() function to convert the string to a number and push it into numberArray.

Finally, we log numberArray to the console to verify that the conversion worked.

Method 2: Using Array.map()#

Another way to convert an array of strings to numbers is by using the Array.map() method. Here is the code:

Text

const stringArray = ['1', '2', '3', '4', '5'];

const numberArray = stringArray.map(function(element) {
  return parseInt(element);
});

console.log(numberArray); // [1, 2, 3, 4, 5]

In this code, we use the Array.map() method to create a new array called numberArray that contains the converted numbers.

The Array.map() method takes a function as an argument. This function is called for each element in the array and returns a new value. In this case, we use the parseInt() function to convert each string to a number.

Finally, we log numberArray to the console to verify that the conversion worked.

Conclusion#

In this post, we learned two ways to convert an array of strings to numbers in JavaScript. The first method uses a for loop to iterate over each element in the array and convert it to a number. The second method uses the Array.map() method to create a new array with the converted numbers.

Both methods are simple and easy to understand, but depending on the context of your program, one method may be more appropriate than the other.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC