integer length in javascript
Well, I don't think providing length properties to number will be helpful. The point is the length of strings does not change by changing its representation.
for example you can have a string similar to this:
var b = "sometext";
and its length property will not change unless you actually change the string itself.
But this is not the case with numbers.
Same number can have multiple representations. E.g.:
var a = 23e-1;
and
var b = 2.3;
So its clear that same number can have multiple representations hence, if you have length property with numbers it will have to change with the representation of the number.
you must set variable toString()
first, like this:
var num = 1024,
str = num.toString(),
len = str.length;
console.log(len);
You can find the 'length' of a number using Math.log10(number)
var num = 1024;
var len = Math.floor(Math.log10(num))+1;
console.log(len);
or if you want to be compatible with older browsers
var num = 1024;
var len = Math.log(num) * Math.LOG10E + 1 | 0;
console.log(len);
The | 0
does the same this as Math.floor.