Skip to content
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

Closed
timfenney opened this issue Jan 12, 2025 · 4 comments · Fixed by #37681
Labels
Content:JS JavaScript docs

Comments

@timfenney
Copy link

timfenney commented Jan 12, 2025

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:

"foo".includes("f") // true

I can even access a property on a number if it has a decimal point:

2.0.toString() // '2'

This works, as expected.

But unexpectedly, I can't access a property on an integral numeric primitive:

2.toString() // error

However I can access it by wrapping the integer in parens (expected):

(2).toString() // => '2'

Here is an unexpected working example with two periods:

2..toString() // '2'

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?

  • An issue where it seems the submitter wasn't helped with this exact edge case
  • The PR for the issue which improved things, but didn't mention this case
  • Numeric Literals section of the ECMAScript specification

Do you have anything more you want to share?

TLDR;

  1. Property access on autoboxed primitives is confusing
  2. We are struggling with an example that "should work"
  3. This edge case is not caused by autoboxing but parsing
  4. I think this is the only such edge case
  5. We might consider updating the Primitives article and this error landing page with a short note explaining that in 2.toString() e.g., 2. is the Number, therefore you need (2).toString() or so

Notes

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:

(2.) // 2

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:
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.

@timfenney timfenney added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 12, 2025
@github-actions github-actions bot added Content:Glossary Glossary entries Content:JS JavaScript docs labels Jan 12, 2025
@timfenney
Copy link
Author

@hamishwillee @Elchi3 you were in the related discussion aha

@Josh-Cena
Copy link
Member

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.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2025
@Josh-Cena Josh-Cena added closed: wontfix Out of scope, too much effort, or working as intended and removed Content:JS JavaScript docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jan 12, 2025
@timfenney
Copy link
Author

timfenney commented Jan 12, 2025

...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.

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,

Variable names can't start with numbers in JavaScript. The following fails:

// ...
alert(1.foo);
// SyntaxError: identifier starts immediately after numeric literal

You will need to rename your variable to avoid the leading number.

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?

@Josh-Cena Josh-Cena reopened this Jan 12, 2025
@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 12, 2025
@Josh-Cena
Copy link
Member

That error page definitely can use some revision to mention accessing properties on number literals!

@Josh-Cena Josh-Cena added Content:JS JavaScript docs and removed closed: wontfix Out of scope, too much effort, or working as intended needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. Content:Glossary Glossary entries labels Jan 12, 2025
@Josh-Cena Josh-Cena changed the title JS Number parsing and auto boxing literal primitives "SyntaxError: identifier starts immediately after numeric literal" should mention property access on number literal Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants