var x = 1234567;

x.toString().length;

This process will only work for positive numbers that do not turn into exponential form with .toString().

Answer from thecodeparadox on Stack Overflow
🌐
IncludeHelp
includehelp.com › code-snippets › how-can-i-find-the-length-of-a-number-in-javascript.aspx
How can I find the length of a number in JavaScript?
Given a number, we have to find its length using JavaScript's "length" property. Submitted by Pratishtha Saxena, on August 14, 2022 · To get the length of a number, we use .length property.
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to get the length of a number in javascript
Easy Ways to Get the Length of a Number in JavaScript
August 11, 2022 - String objects have a length property that returns the number of characters (UTF-16 code units) in a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript | MDN
The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
🌐
Itsourcecode
itsourcecode.com › home › how to get length number javascript? 6 simple steps
How to get length number JavaScript? 6 Simple Steps
August 23, 2023 - Learn how to get length number JavaScript in 6 Simple Steps. This a guide through the process of obtaining the length of a number.
🌐
Shouts
shouts.dev › articles › javascript-get-the-length-number-of-digits-in-a-number
Javascript Get the Length (Number of Digits) in a Number - Shouts.dev
November 20, 2022 - Here what we actually do is, first of all we'll create a method called getLength(), where we'll convert this number into string and then we'll calculate the length of that string. Thus it'll return 5.
Find elsewhere
🌐
Designcise
designcise.com › web › tutorial › how-to-get-the-length-of-an-integer-in-javascript
How to Get the Length of an Integer in JavaScript? - Designcise
September 26, 2022 - You can get the number of digits in a JavaScript number in the following ways: Converting to String and Checking the length; Calculating the Number of Digits; Looping and Removing Digits Off the End. If the number you wish to calculate the length for is very large (e.g. Number.MAX_VALUE), then you should consider converting the number to bigint first.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript length of number | html example code
JavaScript length of number | HTML example code
November 24, 2021 - Then use the length() method to get the length of the number in JavaScript. var x = 123456789; x.toString().length; Complete HTML example code: Note: This process will also work for Float Numbers and for Exponential numbers also.
🌐
Codecademy
codecademy.com › docs › javascript › storage › .length
JavaScript | Storage | .length | Codecademy
July 8, 2025 - Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity. Beginner Friendly.Beginner Friendly15 hours15 hours ... The .length property does not accept any parameters. ... Returns a number representing the count of items or characters, depending on the object it’s used with (e.g., arrays, strings, NodeLists).
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › length
JavaScript String length - Get Length of String | Vultr Docs
April 10, 2025 - This code declares a string ... of characters in the string, which outputs 13. The JavaScript string length function counts all characters, including spaces and punctuation....
Top answer
1 of 16
321

length is a property, not a method. You can't call it, hence you don't need parenthesis ():

function getlength(number) {
    return number.toString().length;
}

UPDATE: As discussed in the comments, the above example won't work for float numbers. To make it working we can either get rid of a period with String(number).replace('.', '').length, or count the digits with regular expression: String(number).match(/\d/g).length.

In terms of speed potentially the fastest way to get number of digits in the given number is to do it mathematically. For positive integers there is a wonderful algorithm with log10:

var length = Math.log(number) * Math.LOG10E + 1 | 0;  // for positive integers

For all types of integers (including negatives) there is a brilliant optimised solution from @Mwr247, but be careful with using Math.log10, as it is not supported by many legacy browsers. So replacing Math.log10(x) with Math.log(x) * Math.LOG10E will solve the compatibility problem.

Creating fast mathematical solutions for decimal numbers won't be easy due to well known behaviour of floating point math, so cast-to-string approach will be more easy and fool proof. As mentioned by @streetlogics fast casting can be done with simple number to string concatenation, leading the replace solution to be transformed to:

var length = (number + '').replace('.', '').length;  // for floats
2 of 16
107

Here's a mathematical answer (also works for negative numbers):

function numDigits(x) {
  return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1;
}

And an optimized version of the above (more efficient bitwise operations): *

function numDigits(x) {
  return (Math.log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}

Essentially, we start by getting the absolute value of the input to allow negatives values to work correctly. Then we run through the log10 operation to give us what power of 10 the input is (if you were working in another base, you would use the logarithm for that base), which is the number of digits. Then we floor the output to only grab the integer part of that. Finally, we use the max function to fix decimal values (any fractional value between 0 and 1 just returns 1, instead of a negative number), and add 1 to the final output to get the count.

The above assumes (based on your example input) that you wish to count the number of digits in integers (so 12345 = 5, and thus 12345.678 = 5 as well). If you would like to count the total number of digits in the value (so 12345.678 = 8), then add this before the 'return' in either function above:

x = Number(String(x).replace(/[^0-9]/g, ''));

* Please note that bitwise operations in JavaScript only work with 32-bit values (max of 2,147,483,647). So don't go using the bitwise version if you expect numbers larger than that, or it simply won't work.

🌐
freeCodeCamp
freecodecamp.org › news › javascript-array-length-tutorial
JavaScript Array Length – How to Find the Length of an Array in JS
September 4, 2024 - In the above code, a variable with the name numbers stores an array of numbers, while the variable numberSize stores the number of elements present in the array by using the method .length.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-get-length-of-number-457ab20c82e3
How to Get the Length of a Number in JavaScript | JavaScript in Plain English
July 11, 2022 - String objects have a length property that returns the number of characters (UTF-16 code units) in a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › length
String: length - JavaScript | MDN
Answer:"; console.log(`${str} ${str.length}`); // Expected output: "Life, the universe and everything. Answer: 42" A non-negative integer. This property returns the number of code units in the string. JavaScript uses UTF-16 encoding, where each Unicode character may be encoded as one or two code units, so it's possible for the value returned by length to not match the actual number of Unicode characters in the string.
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
If you don't specify it, JavaScript will not round the number. toFixed() returns a string, with the number written with a specified number of decimals: let x = 9.656; x.toFixed(0); x.toFixed(2); x.toFixed(4); x.toFixed(6); Try it Yourself » · toFixed(2) is perfect for working with money. toPrecision() returns a string, with a number written with a specified length: let x = 9.656; x.toPrecision(); x.toPrecision(2); x.toPrecision(4); x.toPrecision(6); Try it Yourself » ·