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 a number into an array in JavaScript?
In JavaScript, there are several methods to convert a number into an array of its individual digits. This conversion is useful for tasks like splitting phone numbers, parsing dates, or performing mathematical operations on individual digits.
Method 1: Using split() Method
The split() method is the simplest approach. It converts the number to a string and then splits it into individual characters.
Example
<html>
<body>
<p id="result1"></p>
<script>
let number = 12345;
let numberArray = number.toString().split("");
document.getElementById("result1").innerHTML = "Array: [" + numberArray.join(", ") + "]";
</script>
</body>
</html>
Array: [1, 2, 3, 4, 5]
Method 2: Using Array.from() with Number Constructor
Array.from() creates a new array from an iterable object. Combined with the Number constructor, it converts string digits to numbers.
Example
<html>
<body>
<p id="result2"></p>
<script>
let number = 12345;
let numberArray = Array.from(number.toString(), Number);
document.getElementById("result2").innerHTML = "Array: [" + numberArray.join(", ") + "]";
</script>
</body>
</html>
Array: [1, 2, 3, 4, 5]
Method 3: Using For Loop
A for loop provides direct control over the conversion process, iterating through each digit and converting it to a number.
Example
<html>
<body>
<p id="result3"></p>
<script>
let number = 12345;
let numberArray = [];
let numberString = number.toString();
for (let i = 0; i < numberString.length; i++) {
numberArray.push(parseInt(numberString[i]));
}
document.getElementById("result3").innerHTML = "Array: [" + numberArray.join(", ") + "]";
</script>
</body>
</html>
Array: [1, 2, 3, 4, 5]
Method 4: Using map() Method
The map() method creates a new array by applying a function to each element. Here we use it to convert string digits to numbers.
Example
<html>
<body>
<p id="result4"></p>
<script>
let number = 12345;
let numberArray = number.toString().split("").map(Number);
document.getElementById("result4").innerHTML = "Array: [" + numberArray.join(", ") + "]";
</script>
</body>
</html>
Array: [1, 2, 3, 4, 5]
Method 5: Using Spread Operator
The spread operator provides a concise way to convert a string into an array of characters, which can then be mapped to numbers.
Example
<html>
<body>
<p id="result5"></p>
<script>
let number = 12345;
let numberArray = [...number.toString()].map(Number);
document.getElementById("result5").innerHTML = "Array: [" + numberArray.join(", ") + "]";
</script>
</body>
</html>
Array: [1, 2, 3, 4, 5]
Comparison
| Method | Performance | Readability | Browser Support |
|---|---|---|---|
| split() | Good | High | All browsers |
| Array.from() | Good | High | ES6+ |
| For loop | Best | Medium | All browsers |
| map() | Good | High | ES5+ |
| Spread operator | Good | High | ES6+ |
Conclusion
JavaScript offers multiple methods to convert numbers into arrays. Choose split() or Array.from() for simplicity, map() for functional programming style, or for loops for maximum performance and browser compatibility.
