Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

JavaScript: Difference between toString and toString()

$
0
0

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

Edit epic-thunder-82g9z


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>