
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Number Split into individual digits in JavaScript
We are required to write a JavaScript function that takes in a Number as the only input. The function should simply split the digits of the number and construct and return an array of those digits.
For example −
If the input number is −
const num = 55678;
Then the output should be −
const output = [5, 5, 6, 7, 8];
The only condition is that we cannot convert the Number to String or use any ES6 function over it.
Example
The code for this will be −
const num = 55678; const numberToArray = (num) => { const res = []; while(num){ const last = num % 10; res.unshift(last); num = Math.floor(num / 10); }; return res; }; console.log(numberToArray(num));
Output
And the output in the console will be −
[ 5, 5, 6, 7, 8 ]
- Related Articles
- How to split JavaScript Number into individual digits?
- How to break or split number into individual digits in Excel?
- How can I split an array of Numbers to individual digits in JavaScript?
- How to split a number into digits in R?
- Split number into 4 random numbers in JavaScript
- Split number into n length array - JavaScript
- Sum of individual even and odd digits in a string number using JavaScript
- Split string into groups - JavaScript
- Split a string and insert it as individual values into a MySQL table?
- Split string into equal parts JavaScript
- Can split array into consecutive subsequences in JavaScript
- Split Array of items into N Arrays in JavaScript
- Split keys and values into separate objects - JavaScript
- Finding product of Number digits in JavaScript
- Separating digits of a number in JavaScript

Advertisements