Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100.toString vs 100['toString']

Tags:

javascript

100['toString'] //does not fail
100.toString //fails

why?

100.toString is not same as 100.toString() . So why in the second case I am not getting the function as returned value?

like image 923
Nick Vanderbilt Avatar asked Mar 11 '10 22:03

Nick Vanderbilt


2 Answers

The second line fails because it is parsed as a number "100.", followed by "toString".

To use the dot notation, Any of the following will work:

(100).toString
100.0.toString
100..toString
var a = 100;
a.toString

If you are trying to call the toString function , you will also need to include the parentheses:

(100).toString()
100.0.toString()
100..toString()
var a = 100;
a.toString()

I prefer using parentheses (or a variable, if I already have one obviously), because the alternatives could be confusing and unintuitive.

like image 181
Matthew Crumley Avatar answered Oct 29 '22 16:10

Matthew Crumley


Use (100).toString instead.

like image 5
Gumbo Avatar answered Oct 29 '22 17:10

Gumbo