0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Count Digits in Number

You can count digits in a number in JavaScript by converting it to a string and using number.toString().length or by repeatedly dividing the number by 10 and counting the steps.
📋

Examples

Input12345
Output5
Input0
Output1
Input-9876
Output4
🧠

How to Think About It

To count digits in a number, think about how many individual numbers make it up. One easy way is to turn the number into text and count the characters, ignoring any minus sign. Another way is to keep dividing the number by 10 until it becomes zero, counting how many times you do this.
📐

Algorithm

1
Get the input number.
2
If the number is negative, convert it to positive.
3
If the number is zero, return 1 because zero has one digit.
4
Initialize a count to zero.
5
While the number is greater than zero, divide it by 10 and increase the count by one.
6
Return the count as the number of digits.
💻

Code

javascript
function countDigits(num) {
  num = Math.abs(num);
  if (num === 0) return 1;
  let count = 0;
  while (num > 0) {
    num = Math.floor(num / 10);
    count++;
  }
  return count;
}

console.log(countDigits(12345));
console.log(countDigits(0));
console.log(countDigits(-9876));
Output
5 1 4
🔍

Dry Run

Let's trace counting digits in the number 12345 through the code

1

Start with number

num = 12345

2

Make positive

num = 12345 (already positive)

3

Check zero

num !== 0, continue

4

Initialize count

count = 0

5

Divide and count loop

num = Math.floor(12345 / 10) = 1234, count = 1

6

Next loop

num = Math.floor(1234 / 10) = 123, count = 2

7

Next loop

num = Math.floor(123 / 10) = 12, count = 3

8

Next loop

num = Math.floor(12 / 10) = 1, count = 4

9

Next loop

num = Math.floor(1 / 10) = 0, count = 5

10

Loop ends

num = 0, return count = 5

num (after division)count
12341
1232
123
14
05
💡

Why This Works

Step 1: Convert to positive

Using Math.abs() ensures the number is positive so the minus sign doesn't affect digit count.

Step 2: Handle zero

Zero is a special case because it has one digit, so we return 1 immediately.

Step 3: Count digits by division

Dividing the number by 10 removes the last digit each time, so counting how many times we do this tells us the number of digits.

🔄

Alternative Approaches

String conversion
javascript
function countDigits(num) {
  return num.toString().replace('-', '').length;
}

console.log(countDigits(12345));
console.log(countDigits(0));
console.log(countDigits(-9876));
This method is simple and fast for most cases but uses string conversion which may be slower for very large numbers.
Using logarithm
javascript
function countDigits(num) {
  num = Math.abs(num);
  if (num === 0) return 1;
  return Math.floor(Math.log10(num)) + 1;
}

console.log(countDigits(12345));
console.log(countDigits(0));
console.log(countDigits(-9876));
This method uses math functions and is efficient but requires handling zero separately.

Complexity: O(log n) time, O(1) space

Time Complexity

The loop divides the number by 10 each time, so it runs proportional to the number of digits, which is about log base 10 of the number.

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

The logarithm method is fastest for large numbers, string conversion is simplest, and division loop is easy to understand and reliable.

ApproachTimeSpaceBest For
Division loopO(log n)O(1)Understanding and no string use
String conversionO(n)O(n)Simplicity and readability
LogarithmO(1)O(1)Performance with large numbers
💡
Use Math.abs() to handle negative numbers before counting digits.
⚠️
Counting the minus sign as a digit when the number is negative.