Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert array of strings to array of numbers in JavaScript?
In JavaScript, there are different ways to convert an array of strings to an array of numbers. The most common approaches include using the built-in parseInt() method, the Number() constructor, and the unary + operator.
Using the parseInt() Method
The parseInt() method is a built-in function in JavaScript that takes a string as an argument and returns an integer. This method can be used with the map() method to convert an array of strings to an array of numbers.
Syntax
parseInt(string, radix);
Parameters
-
String ? The string to be converted to a number.
-
Radix ? An optional argument that specifies the base (radix) of the string. The default value is 10.
Example
The following example shows how to use the parseInt() method to convert an array of strings to an array of numbers.
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result"></div>
<script>
var arr = ["1", "2", "3"];
// array of strings
var nums = arr.map(function(str) {
// using map() to convert array of strings to numbers
return parseInt(str);
});
document.getElementById("result").innerHTML = nums;
</script>
</body>
</html>
1,2,3
In the above example, the parseInt() method is used within the map() method to convert each element in the arr array to a number. The map() method creates a new array with the results of calling a function for each element in the array.
Using the Number() Constructor
Another way to convert an array of strings to an array of numbers is to use the Number() constructor. This approach handles decimal numbers better than parseInt().
Example
In the below example, the Number() constructor is used to convert an array of strings to an array of numbers.
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result"></div>
<script>
function toNumber(value) {
return Number(value);
}
var arr = ["1", "2", "3.5"];
var nums = arr.map(toNumber);
document.getElementById("result").innerHTML = nums;
</script>
</body>
</html>
1,2,3.5
In the above example, the toNumber() function converts a string to a number using the Number() constructor. This function preserves decimal values unlike parseInt().
Using the Unary + Operator
Another way to convert an array of strings to an array of numbers is to use the unary + operator. The unary + operator is a prefix operator that converts its operand to a number.
Example
The below program shows how to use the unary + operator to convert an array of strings to an array of numbers.
<!doctype html>
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result"></div>
<script>
var arr = ["1", "2", "3"];
// Using the unary + operator
var nums = arr.map(unaryOp);
document.getElementById("result").innerHTML = nums;
// Function that converts string to number
function unaryOp(value) {
return +value;
}
</script>
</body>
</html>
1,2,3
In the above example, the unaryOp() function is a user-defined function that uses the unary + operator to convert a string to a number. This approach is concise and handles both integers and decimals.
Comparison
| Method | Handles Decimals? | Performance | Best For |
|---|---|---|---|
parseInt() |
No - truncates | Fast | Integers only |
Number() |
Yes | Good | All number types |
Unary +
|
Yes | Fastest | Clean, concise code |
Conclusion
JavaScript offers multiple ways to convert string arrays to number arrays. Use Number() or unary + for decimal preservation, and parseInt() when you need only integers. The map() method is essential for applying the conversion to each array element.
