# How to convert a number into array in JavaScript ?

• Last Updated : 26 Jul, 2021

We have given a number and the task is to convert the given number into an array using JavaScript.

Examples:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

```Input: 235283
Output: [2, 3, 5, 2, 8, 3]

Input: 8998123
Output: [8, 9, 9, 8, 1, 2, 3]

Input: 1234567
Output: [1, 2, 3, 4, 5, 6, 7]```

In this article, we will use two different methods to convert the given number into an array.

Method 1: Using Array.from() Method: The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object.

Syntax :

`Array.from(object, mapFunction, thisValue)`

Approach:

1. Store a number in a variable.
2. Use Array.from() method and in its first parameter enter the string type cast value.
3. In the second parameter, we use a function i.e. myFunc, at every iteration the function will be called.
4. The myFunc function will take a parameter that was returned by the iteration of Array.from() method. We typecast the number into a string so the type of parameter is a string, but we will typecast it into integer and return it.
5. The value returned by the Array.from() is our result.

Example:

## Javascript

 `var` `myInt = 235345;`` ` `// Getting the string as a parameter``// and typecasting it into an integer``let myFunc = num => Number(num);`` ` `var` `intArr = Array.from(String(myInt), myFunc);`` ` `// Print the result array``console.log(intArr);`

Output:

`[2, 3, 5, 3, 4, 5 ]`

Method 2: Using map() Method: The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally, map() method is used to iterate over an array and calling a function on every element of the array.

Syntax:

`array.map(function(currentValue, index, arr), thisValue)`

Approach:

1. Store the integer value in a variable.
2. Typecast the integer into a string.
3. Using the split() method to make it an array of strings.
4. Iterate over that array using the map() method.
5. Using the map() method returns the array of strings into an array of Integers.

## Javascript

 `// Declare a variable and store an``// integer value``var` `num = 235345`` ` `// Here we typecasting the num``// Splitting the num, so that``// we got an array of strings``// Then use map function to``// convert the array of strings``// into array of numbers`` ` `var` `myArr = String(num).split(``""``).map((num)=>{``  ``return` `Number(num)``})`` ` `console.log(myArr)`

Output:

`[2, 3, 5, 3, 4, 5]`

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

My Personal Notes arrow_drop_up