-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Improve var scoping/hoisting description #28068
Conversation
@@ -81,7 +84,7 @@ The list that follows the `var` keyword is called a _{{glossary("binding")}} lis | |||
|
|||
### Hoisting | |||
|
|||
`var` declarations, wherever they occur, are processed before any code is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function or global code. | |||
`var` declarations, wherever they occur, are processed during JavaScript compilation before any code is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function or global code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I try to avoid the concept of "JavaScript compilation" because it's very ambiguous. Is it JIT? Is it realm initialization? Is it source parsing? In fact, var
declarations are processed as the first step of execution, just before user-visible evaluation. See for examle: https://tc39.es/ecma262/#sec-source-text-module-record-initialize-environment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest changes to "Description" resolve that part of the issue I was concerned about.
For Hoisting I'm happy to leave out "JavaScript Compilation" altogether. The version below changes
- the first sentence to imply "script" means lines of code within a single script source.
- the last sentence to not say that hoisting may move declarations to the top of global code.
### Hoisting
var declarations, wherever they occur in a script, are processed before any code for the script is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called hoisting, as it appears that the variable declaration is moved to the top of the function, static initialization block or script source in which it occurs.
[Sorry about proposing the update here if it should have been done as an edit to the PR - this is my first experience of Git work flow.]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`var` declarations, wherever they occur, are processed during JavaScript compilation before any code is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function or global code. | |
`var` declarations, wherever they occur in a script, are processed before any code within the script is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function, static initialization block, or script source in which it occurs. |
So you are proposing the change above? This is okay for me. You can click the Commit suggestion button.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact I suggest adding another note to specifically point out about multiple scripts:
`var` declarations, wherever they occur, are processed during JavaScript compilation before any code is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function or global code. | |
`var` declarations, wherever they occur in a script, are processed before any code within the script is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called [_hoisting_](/en-US/docs/Glossary/Hoisting), as it appears that the variable declaration is moved to the top of the function, static initialization block, or script source in which it occurs. | |
> **Note:** `var` declarations are only hoisted to the top of the current script. If you have two `<script>` elements within one HTML, the first script cannot access variables declared by the second one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I committed the last suggestion. And immediately noticed the note could be misconstrued to mean "ever". Here's my suggestion to clarify the note:
Note:
var
declarations are only hoisted to the top of the current script. If you have two<script>
elements within one HTML, the first script cannot access variables declared by the second one when first executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even better:
**Note:
var
declarations are only hoisted to the top of the current script. If you have two <script> elements within one HTML, the first script cannot access variables declared by the second before the second script has been processed and executed.
Co-authored-by: Joshua Chen <[email protected]>
@domleonard Is this ready for merge? |
Preview URLs (comment last updated: 2023-07-20 14:37:44) |
The preview looks great and I'm happy with merging it. Thank you for all the effort. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Thank you for the nice work and congrats on your first contribution here—welcome aboard!
Description
Remove ambiguity in
var
Description that can be construed to mean standard scripts have their own scope.Expand the first sentence of
var
Hoisting to explicitly say that Hoisting occurs during JavaScript compilation.Motivation
Changes to the description are aimed at preventing readers from inferring that individual scripts have "script" scope.
The change to
var
Hoisting is to explicitly indicate thatvar
declarations (and hoisting) don't take effect until the script is compiled - in words that do not touch on the semantics of host environment support for separate compilation of one or more script sources.See Issue 27966 for a case study (on Stack Overflow) involving both of these proposals.
Related Articles
Related issues and pull requests
Fixes #27966