Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between numbers using or without using variable

Tags:

javascript

What's the difference between the following code?

var a = 1;
a.toString(); // outputs: "1"

But this throws an error:

1.toString();

SyntaxError: Unexpected token ILLEGAL

Why? Why does the first code work but the second code throws an error?

like image 800
Navin Rauniyar Avatar asked Dec 12 '22 03:12

Navin Rauniyar


1 Answers

With method invocations, it is important to distinguish between the floating-point dot and the method invocation dot. Thus, you cannot write 1.toString(); you must use one of the following alternatives:

1..toString()
1 .toString() //space before dot
(1).toString()
1.0.toString()
like image 145
Bhojendra Rauniyar Avatar answered Feb 09 '23 01:02

Bhojendra Rauniyar