I'm struggling to understand this JavaScript behaviour (javascript n00b alert!!!). Source of the question is a typo in my code which led to the undesired situation. What I wanted to know is the length of a certain number. For example, if its 100, then answer should be 3, if 5893 then answer should be 4 and so on. To achieve this what I did simply is convert number to a string and then invoke .length
on the string.
private getNumberLength(num: number) {
return num.toString().length;
}
In the above return
statement, I had typo such that it looked like
return num.toString.length;
The result was no compilation error and getNumberLength
always returned 1
. I fail to understand this (why 1
?). Can somebody please help me understand this?
Below you can quickly test if you wish