Split A Number Into An Array In JavaScript

To split a number into an array in JavaScript, you can use any of these methods –

  1. Use the Array.from() method and pass two arguments, one is String(number) and second is a map function that converts each String element in the array back to a number.
  2. Use the Array.split() method on String(number) and use the Array.map() method to convert each element of the String array to a number.
  3. Use the Array.split() method on String(number) and use the Array.forEach() method to convert each element of the String array to a number.

Let’s discuss both the methods in detail.

Split A Number Into An Array In JavaScript

split a number into an array in javascript

Method 1: Use The Array.from() Method And Pass Two Arguments

We can use the Array.from() method to split a number into an array in JavaScript. The Array.from() method creates a new array instance from an array-like or iterable object. It has an optional map function that can be used to convert each element of the array.

The first argument of the Array.from() method is the number that needs to be split into an array. The second argument is a map function that converts each String element in the array back to a number.

We can use the Array.split() method on String(number) to split it into an array of strings. Then, we can use the Array.map() method to convert each element of the String array to a number.

Here is the example –

var number = 12345;

var arr = Array.from(String(number), Number);

console.log(arr);

In the above code, we have first converted the number to a String using the String() method. Then, we have used the Array.from() method to split it into an array of strings. We have passed two arguments to the Array.from() method –

The first argument is String(number). It is the number that needs to be split into an array.

The second argument is a map function that converts each string element in the array back to a number. In this map function, we have used the Number() method to convert each string element to a number. Finally, we have logged the output to the console.

Output:

[1, 2, 3, 4, 5]

We could have also first created a String array and then used the Array.map() method to convert each String element to a number in the resulting array.

Here is the code for that –

var number = 12345;

var arr = Array.from(String(number));

var newArray = arr.map(element => Number(element));

console.log(newArray);

In the above code, we have first converted the number to a String using the String() method. Then, we have used the Array.from() method to split it into an array of strings. We have stored this array in a variable named arr.

After that, we have used the Array.map() method on the arr variable. The Array.map() method creates a new array with the results of calling a function for every array element. In this function, we have used the Number() method to convert each string element to a number. We have stored this new array in a variable named newArray. Finally, we have logged the output to the console.

Output: [1, 2, 3, 4, 5]

Method 2: Use The Array.split() Method On String(number) And Use The Array.map() Method

We can also use the Array.split() method on String(number) to split it into an array of strings. Then, we can use the Array.map() method to convert each element of the String array to a number.

Here is the code for that –

var number = 12345;

var arr = String(number).split("");

var newArray = arr.map(element => Number(element));

console.log(newArray);

In the above code, we have first converted the number to a String using the String() method. Then, we have used the Array.split() method on this String to split it into an array of strings. We have stored this array in a variable named arr.

After that, we have used the Array.map() method on the arr variable. The Array.map() method creates a new array with the results of calling a function for every array element. In this function, we have used the Number() method to convert each string element to a number. We have stored this new array in a variable named newArray. Finally, we have logged the output to the console.

Output:

[1, 2, 3, 4, 5]

Method 3: Use The Array.forEach() Method To Iterate Over The String Array And Convert Each Character To A Number

We can also use the Array.forEach() method to iterate over the String and convert each character to a number.

Here is the code for that –

var number = 12345;

var arr = [];

String(number).split("").forEach(element => {

  arr.push(Number(element));

});

console.log(arr);

In the above code, we have first converted the number to a String using the String() method. Then, we have used the Array.split() method on this String to split it into an array of strings. We have stored this array in a variable named arr.

After that, we have used the Array.forEach() method on the arr variable. The Array.forEach() method executes a function for each element in an array. In this function, we are using the Number() method to convert each string element to a number and then pushing it into the arr array. Finally, we are logging the output to the console.

Output:

[1,2,3,4,5]

These are some of the ways in which you can split a number into an array in JavaScript. I hope this was helpful.

Leave a Reply