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

Issues with "Description" and "Hoisting" in var documentation. #27966

Closed
domleonard opened this issue Jul 15, 2023 · 1 comment · Fixed by #28068
Closed

Issues with "Description" and "Hoisting" in var documentation. #27966

domleonard opened this issue Jul 15, 2023 · 1 comment · Fixed by #28068
Labels
Content:JS JavaScript docs

Comments

@domleonard
Copy link
Contributor

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

What specific section or headline is this issue about?

Description and Hoisting sections

What information was incorrect, unhelpful, or incomplete?

  1. The Description section states

The scope of a variable declared with var is one of the following curly-brace-enclosed syntaxes that most closely contains the var statement:

Or the current module or script, if it's contained in neither of these.)

The issue: Scripts that are not modules don't create their own variable scope;

  1. The Hoisting section states in the second sentence

Declaring a variable anywhere in the code is equivalent to declaring it at the top.

The issue The "top" of what is not defined and could benefit from including a "top of the containing script" option.

What did you expect to see?

under Description

Or the current module or script, if it's contained in neither of these.

might benefit from being reworded along the lines of:

Or if it's not contained in either of these,

  • the module a var is declared within, or
  • global scope if the declaration is not within a module.

Second sentence of Hoisting

Declaring a variable anywhere in the code is equivalent to declaring it at the top.

might benefit from being reworded along the lines of:

Declaring a variable anywhere in the code is equivalent to declaring it at the top of the function, static initialization block or script it is declared in.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

The reason for suggestions in this request comes from this Stack Overflow question about why a global variable in a script is not hoisted such that it can be accessed in a preceding script without generating a run time error.

The obvious answer about a timing error - the second script tag had not been encountered or processed at the time the first script was executed - doesn't appear to be covered in question answers, the nominated duplicate question on Stack Overflow, or in MDN articles for var (this amendment request) or the glossary definition for Hoisting.

MDN metadata

Page report details
@domleonard domleonard added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jul 15, 2023
@github-actions github-actions bot added Content:Glossary Glossary entries Content:JS JavaScript docs labels Jul 15, 2023
@Josh-Cena
Copy link
Member

Josh-Cena commented Jul 15, 2023

You are totally right—interaction of multiple scripts is very tricky and it's not documented here, because it's not JS semantics but HTML semantics. Still it's worth mentioning.

Do you want to submit a PR? I can review.

@Josh-Cena Josh-Cena removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. Content:Glossary Glossary entries labels Jul 15, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Content:JS JavaScript docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants