-
Notifications
You must be signed in to change notification settings - Fork 22.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"SyntaxError: identifier starts immediately after numeric literal" should mention property access on number literal #37613
Comments
@hamishwillee @Elchi3 you were in the related discussion aha |
There is no difference between "accessing property on a literal" and "accessing property on a variable". This is a syntactic difference, not a semantic difference. The error you are running into is a pure syntactic issue; it has nothing to do with autoboxing whatsoever, and I fail to see why it should be mentioned in an article about a runtime entity. Doing so just muddies the water on our discussion topic. This gotcha is mentioned in articles about syntax, including https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors. |
I'm right there with you. My perspective is more about what is pragmatic. I almost ended up leaving an issue asking for clarification like the first one. Maybe the Primitive article is a bad fit. The "landing page" for the exception I linked earlier shows some examples that will error, but the explanation is bad. It says,
The previous examples in the snippet used variables, but this is confusing advice at best. (The alert is called on a literal, not a variable.) Can we consider an adjustment to this section? |
That error page definitely can use some revision to mention accessing properties on number literals! |
MDN URL
https://developer.mozilla.org/en-US/docs/Glossary/Primitive
What specific section or headline is this issue about?
Primitive
What information was incorrect, unhelpful, or incomplete?
The primitives article says properties can be looked up on primitives.
The article doesn't mention a weird case which has been confusing people.
What did you expect to see?
The docs explain I can access a property on a primitive literal, giving a string as an example:
I can even access a property on a number if it has a decimal point:
This works, as expected.
But unexpectedly, I can't access a property on an integral numeric primitive:
However I can access it by wrapping the integer in parens (expected):
Here is an unexpected working example with two periods:
In summary, I expected the general advice to work for all cases. Knowing what I know now, I think it may be worth a short note with an example for integral Numbers.
Do you have any supporting links, references, or citations?
Do you have anything more you want to share?
TLDR;
2.toString()
e.g.,2.
is the Number, therefore you need(2).toString()
or soNotes
Property access on a primitive results in an object autoboxing it. This results in a temporary object to supply the properties. Because the object is ephemeral, any changes to it can't be observed (reads seem to go to a new object). This preserves immutability.
In the linked issue, the submitter was confused by this very edge case. The discussion didn't address the submitter's problem. The cause was supposed to be autoboxing, but that was a red herring.
AFAICT this is an effect of parsing. Check this out:
This is a valid Number literal. The Numeric Literal parsing grammar rule "eats" the
.
as part of the Number. This explains the confusing exception message in Firefox: SyntaxError: identifier starts immediately after numeric literal.Here is are the relevant rules from the spec's BNF grammar:

This results in an error in nodejs: "Uncaught SyntaxError: Invalid or unexpected token". In the Firefox console, the error is "SyntaxError: identifier starts immediately after numeric literal", and beside it there will be a link [Learn More].
I did a brief skim through the grammar rules in the 2024 and 2015.1 versions of the spec, but I couldn't find any more rules where the RHS of any rule ended in a period. So I think this is the only such edge case.
The text was updated successfully, but these errors were encountered: