JavaScript syntax: Difference between revisions

Content deleted Content added
Synoli (talk | contribs)
m typeof->type of - Fix a typo in one click
Line 100:
Variables declared with <code>var</code> are [[lexical scoping|lexically scoped]] at a [[function scope|function level]], while ones with <code>let</code> or <code>const</code> have a [[block scope|block level]] scope. Since declarations are processed before any code is executed, a variable can be assigned to and used prior to being declared in the code.<ref>{{cite web |title=JavaScript Hoisting |publisher=[[W3Schools]] |url=https://www.w3schools.com/js/js_hoisting.asp |quote=In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.}}</ref> This is referred to as ''{{visible anchor|hoisting}}'', and it is equivalent to variables being [[Forward declaration|forward declared]] at the top of the function or block.<ref>"[http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html JavaScript Scoping and Hoisting]", [http://www.adequatelygood.com/about.html Ben Cherry], ''[http://www.adequatelygood.com/ Adequately Good],'' 2010-02-08</ref><!-- Might not explain scoping very well -->
 
With <code>var</code>, <code>let</code>, and <code>const</code> statements, only the declaration is hoisted; assignments are not hoisted. Thus a {{code|lang=javascript|code=var x = 1}} statement in the middle of the function is equivalent to a {{code|lang=javascript|code=var x}} declaration statement at the top of the function, and an {{code|lang=javascript|code=x = 1}} assignment statement at that point in the middle of the function. This means that values cannot be accessed before they are declared; [[forward reference]] is not possible. With <code>var</code> a variable's value is <code>undefined</code> until it is initialized. Variables declared with <code>let</code> or <code>const</code> cannot be accessed until they have been initialized, so referencing such variables before will cause an error.<!-- Some sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting. Referencing a let or const will not throw if there's a typeoftype of. An exception to the typeof exception is "const x = x;", which you can try in the console. -->
 
Function declarations, which declare a variable and assign a function to it<!-- Function technically isn't actually a type. They're objects which can be executed in a subroutine. -->, are similar to variable statements, but in addition to hoisting the declaration, they also hoist the assignment – as if the entire statement appeared at the top of the containing function – and thus forward reference is also possible: the ___location of a function statement within an enclosing function is irrelevant. This is different from a function expression being assigned to a variable in a <code>var</code>, <code>let</code>, or <code>const</code> statement.