You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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?
The issue: Scripts that are not modules don't create their own variable scope;
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
might benefit from being reworded along the lines of:
Second sentence of Hoisting
might benefit from being reworded along the lines of:
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
en-us/web/javascript/reference/statements/var
The text was updated successfully, but these errors were encountered: