How to convert array of strings to array of numbers in JavaScript ?
Last Updated :
23 Sep, 2024
Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applications.
Input: ["1","2","3","4","5"]
Output: [1,2,3,4,5]
Input: ["10","21","3","14","53"]
Output: [10,21,3,14,53]
There are two methods to do this, which are given below:
Array traversal and typecasting
In this method, we traverse an array of strings and add it to a new array of numbers by typecasting it to an integer using the parseInt() function.
Example: In this example, we converts an array of strings into an array of numbers using the parseInt() function within a loop. It pushes each converted integer into a new array and then prints it.
JavaScript
// Create an array of string
let stringArray = ["1", "2", "3", "4", "5"];
// Create an empty array of number
let numberArray = [];
// Store length of array of string
// in letiable length
length = stringArray.length;
// Iterate through array of string using
// for loop
// push all elements of array of string
// in array of numbers by typecasting
// them to integers using parseInt function
for (let i = 0; i < length; i++)
// Instead of parseInt(), Number()
// can also be used
numberArray.push(parseInt(stringArray[i]));
// Print the array of numbers
console.log(numberArray);
Using map() method of JavaScript
Javascript map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally, the map() method is used to iterate over an array and call function on every element of the array.
Example: In this example, we converts an array of strings into an array of numbers using the map() function, which applies the Number constructor to each element, then prints the resulting array of numbers.
JavaScript
// Create an array of string
let stringArray = ["10", "21", "3", "14", "53"];
// Create a numberArray and using
// map function iterate over it
// and push it by typecasting into
// int using Number
let numberArray = stringArray.map(Number);
// Print the array of numbers
console.log(numberArray);
Output[ 10, 21, 3, 14, 53 ]
Using forEach loop of JavaScript
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
Example: In this example, we converts an array of strings into numbers using the forEach() loop. It iterates over each string, typecasts it into a number using the + operator, and then prints the array.
JavaScript
// Create an array of string
let stringArray = ["10", "21", "3", "14", "53"];
// Create a numberArray and using
// forEach loop iterate over it
// and push it to numberArray with typecasting it into
// int using + operator
let numberArray = [];
stringArray.forEach( ele => numberArray.push(+ele))
// Print the array of numbers
console.log(numberArray);
Output[ 10, 21, 3, 14, 53 ]
Using reduce method of JavaScript
The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Example: In this example, we converts an array of strings into numbers using the reduce() function. It iterates through each string, typecasts it using the + operator, stores the results in an array, and prints it.
JavaScript
// Create an array of string
let stringArray = ["10", "21", "3", "14", "53"];
// Create a numberArray and using
// reduce function iterate over it
// and push it to numberArray with typecasting it into
// int using + operator
let numberArray = stringArray.reduce( (acc, x ) => acc.concat(+x), [])
// Print the array of numbers
console.log(numberArray);
Output[ 10, 21, 3, 14, 53 ]
Using a for loop
To convert an array of strings to numbers using a for loop, iterate through each string, convert it to a number using Number(), and push it to a new array. This method provides control and flexibility for handling each element individually.
Example: In this example we converts an array of strings into an array of numbers using a for
loop. It iterates through each string, converts it to a number using Number()
, and then prints the result.
JavaScript
const stringArray = ["1", "2", "3", "4", "5"];
const numberArray = [];
for (let i = 0; i < stringArray.length; i++) {
numberArray.push(Number(stringArray[i]));
}
console.log(numberArray);
Similar Reads
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
3 min read
How to Convert String to Array of Objects JavaScript ?
Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read
How to convert Integer array to String array using JavaScript ?
The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:Table of ContentApproach 1: using JavaScript array.map() and toString() methodsApproach 2: Us
2 min read
Convert a Number to a String in JavaScript
These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a
1 min read
How to convert a map to array of objects in JavaScript?
A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Find the Sum of an Array of Numbers in JavaScript ?
To find the sum of an array of numbers in JavaScript, you can use several different methods.1. Using Array reduce() MethodThe array reduce() method can be used to find the sum of array. It executes the provided code for all the elements and returns a single output.Syntaxarr.reduce( callback( acc, el
2 min read
How to Convert an Array of Objects to Map in JavaScript?
Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon
3 min read
How to Convert a Map to JSON String in JavaScript ?
A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A
2 min read
Convert a String to Number in JavaScript
To convert a string to number in JavaScript, various methods such as the Number() function, parseInt(), or parseFloat() can be used. These functions allow to convert string representations of numbers into actual numerical values, enabling arithmetic operations and comparisons.Below are the approache
4 min read
How to Convert JSON to string in JavaScript ?
In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read