Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  149] [ 2]  / answers: 1 / hits: 45889  / 11 Years ago, fri, august 9, 2013, 12:00:00

Stoopid question time!



I know that in JavaScript you have to convert the integer to a string:



var num = 1024;
len = num.toString().length;
console.log(len);


My question is this: Why is there no get length property for integers in JavaScript? Is it something that isn't used that often?


More From » string

 Answers
17

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.


[#76438] Thursday, August 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;