• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Split a Number into Digits in JavaScript

How to Split a Number into Digits in JavaScript

June 8, 2022 Leave a Comment

There are a number of ways we can split a number into digits in JavaScript. One of the simplest ways to do this is to convert the number into a string, iterate over it, and create a new array of digits. Here is the code of how to do this, and then we will explain what is going on afterward:

var someNumber = 5436;
var numberToString = someNumber.toString();

var digits = [];
for( var i=0; i<numberToString.length; i++ ){
  digits[i] = Number(numberToString.charAt(i));
}

In the code above, you will notice that we first convert the number to a string using the toString() method. We then make use of the charAt() method to get each individual digit, and then convert that digit back to a number using the Number() method, and finally add that number to our array. And that’s it.

Let’s put our code above into a function to make it really easy to split a number into digits.

function splitNumberToDigits(num){
  var numberToString = num.toString();
  var digits = [];
  for( var i=0; i<numberToString.length; i++ ){
    digits[i] = Number(numberToString.charAt(i));
  }
  return digits;
};

And now, lets show an example of this function to see it in action:

function splitNumberToDigits(num){
  var numberToString = num.toString();
  var digits = [];
  for( var i=0; i<numberToString.length; i++ ){
    digits[i] = Number(numberToString.charAt(i));
  }
  return digits;
};

console.log(splitNumberToDigits(100));
console.log(splitNumberToDigits(213));

#Output:
[1, 0, 0]
[2, 1, 3]

Let’s go over another way to do this that requires a lot less code.

Using the Map Method and Spread Operator to Split a Number into Digits in JavaScript

Another really simple way that requires less code to split a number into digits in JavaScript is to make use of the map() method, the … spread operator, and String() method.

Here is the simple code that split a number into digits using these methods.

var digitsArray = [...String(5463)].map(Number);

Where 5463 in the code above can be any number you want.

Let’s put this in a function and show this in action with a couple of examples:

function splitNumberToDigits(num){
  return [...String(num)].map(Number);
};

console.log(splitNumberToDigits(100));
console.log(splitNumberToDigits(213));

#Output:
[1, 0, 0]
[2, 1, 3]

Hopefully this article has been useful for you to learn how to split a number into digits in JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript value – Get the Value from an Input Field
  • 2.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 3.  Using JavaScript to Add to Array
  • 4.  for each char in string – How to Loop Over Characters of String in JavaScript
  • 5.  Using JavaScript to Remove All Non-Alphanumeric Characters From a String
  • 6.  JavaScript Array includes Method
  • 7.  Using JavaScript to Get the Page Title
  • 8.  Using Javascript to Check if Value is Not Null
  • 9.  Convert a Set to Array in JavaScript
  • 10.  JavaScript Random Number – How to Generate a Random Number In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy