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 first way is to use the built-in parseInt() method, and the second way is to use a type conversion function, and the third one is to use 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 a number. This method can be used to convert an array of strings to an array of numbers.

Syntax

The syntax of the parseInt() method is as follows −

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>

In the above example, the parseInt() method is used in 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 a type conversion function

Another way to convert an array of strings to an array of numbers is to use a type conversion function. The type conversion function is a user-defined function that converts a data type to another data type.

Example

In the below example, a type conversion function 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"];
      var nums = arr.map(toNumber);
      document.getElementById("result").innerHTML = nums;
   </script>
</body>
</html>

In the above example, the toNumber() function converts a string to a number. This function is used to convert an array of strings to an array of numbers.

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>

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 function can be used to convert an array of strings to an array of numbers.

Conclusion

In JavaScript, there are two ways to convert an array of strings to an array of numbers. The first way is to use the built-in parseInt() method, and the second way is to use a type conversion function.

Updated on: 15-Sep-2023

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements