8

Why does 2..toString() return 2 but 2.toString() throws this error?

Example:

console.log(2..toString()); // prints 2

// Firefox throws the error
// `SyntaxError: identifier starts immediately after numeric literal`
console.log(2.toString());

var x = 2;
console.log(x.toString()); // prints 2

// Firefox throws the error
//`TypeError: XML descendants internal method called on incompatible Number`
console.log(x..toString());
Larry Battle
  • 9,008
  • 4
  • 41
  • 55

5 Answers5

8

That's because 2. is parsed as 2.0, so 2..toString() is equivalent to 2.0.toString(), which is a valid expression.

On the other hand, 2.toString() is parsed as 2.0toString(), which is a syntax error.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • `2.` yields `2` in Chromium's console. –  Mar 17 '13 at 08:14
  • @G.Kayaalp, yup, both `2 === 2.0` and `2. === 2.0` are `true`. – Frédéric Hamidi Mar 17 '13 at 08:15
  • hate to be the devil's advocate here, but `2. === 2` is `true` as well – Jan Groth Mar 17 '13 at 08:16
  • AFAIK JS does not have distinct types for integers and decimals. It has the `"number"` type, which is a 64 bit floating point number. –  Mar 17 '13 at 08:20
  • @G.Kayaalp, yes, you're right. That's way your console shows `2.0` as `2`, because the two expressions have both the same value and the same type. The same goes for `2.` versus `2`. – Frédéric Hamidi Mar 17 '13 at 08:21
  • @FrédéricHamidi does `2` is also parsed as `2.0` ? – Royi Namir Mar 17 '13 at 08:43
  • @Royi, nope, `2` without a trailing period is just parsed as `2`. However, add a trailing period and the parser will think it delimits the decimal part of the number, even if the programmer intended it to be the member resolution operator. – Frédéric Hamidi Mar 17 '13 at 08:44
6

2 is just a number, it doesn't have any methods to call.

2. can be coerced into a string, which is an object (i.e. '2.0'), hence can have the method.

Just 2.toString() will be parsed as 2.0tostring(), which of course doesn't make sense.

Looking at how the two are parsed:

enter image description here

vs

enter image description here

The tool to generate these is here by the way: http://jsparse.meteor.com/

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • **Found it .the tool is here (http://jsparse.meteor.com/)** – Royi Namir Mar 17 '13 at 08:32
  • 2
    can you please explain how the diagrams indicates that te first one is wrong ? both seems to be in a number box. I cant see how the diagrams helps.... can you please reply? – Royi Namir Mar 17 '13 at 08:37
  • Sorry, had to get the kids breakfast halfway through – I agree, not sure the diagrams represent things correctly. It's a duplicate anyway, so perhaps ignore my answer. – Rich Bradshaw Mar 17 '13 at 08:56
5
2.toString()

The interpreter sees 2 and thinks, "oh, a number!" Then, it sees the dot and thinks, "oh, a decimal number!" And then, it goes to the next character and sees a t, and it gets confused. "2.t is not a valid decimal number," it says, as it throws a syntax error.


2..toString()

The interpreter sees 2 and thinks, "oh, a number!" Then, it sees the dot and thinks, "oh, a decimal number!" Then, it sees another dot and thinks, "oh, I guess that was the end of our number. Now, we're looking at the properties of this object (the number 2.0)." Then, it calls the toString method of the 2.0 object.

Casey Chu
  • 25,069
  • 10
  • 40
  • 59
2

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString

As the Number object overrides the toString method of the Object object, you first have to explicity use paranthesis to indicate that it is a number, and not an object.

My guess is that 2. implicitly defines it as a float, which is then able to use the .toString() method of the Number object, and not the method of the Object object.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
1

2..toString() will be interpreted as 2.0.toString().

Actually, 2. is a number: console.log(typeof 2.); will be give: number

Iswanto San
  • 18,263
  • 13
  • 58
  • 79