JavaScript syntax: Difference between revisions

Content deleted Content added
Fixed typo
Tags: canned edit summary Mobile edit Mobile app edit iOS app edit
Whitespace and semicolons: big overall change made of quite a few small stuff
Tags: nowiki added Visual edit
Line 25:
 
===Whitespace and semicolons===
[[Space (punctuation)|Space]]s, [[tab character|tab]]s and [[newline]]s used outside of string constants are called [[whitespace (computer science)|whitespace]]. Unlike in [[C (programming language)|C]], whitespace in JavaScript source can directly impact [[Semantics (computer science)|semantics]]. Because[[Semicolon]]<nowiki/>s ofend astatements techniquein calledJavaScript. Because of "automatic [[semicolon insertion]]" (ASI), some statements that are well formed when a newline is parsed will be considered complete, (as if a semicolon were inserted just prior to the newline). Some authorities advise supplying statement-terminating semicolons explicitly, because it may lessen unintended effects of the automatic semicolon insertion.<ref>{{cite book
|title=JavaScript: The definitive Guide
|url=https://archive.org/details/javascript00libg_297
Line 38:
There are two issues: five tokens can either begin a statement or be the extension of a complete statement; and five restricted productions, where line breaks are not allowed in certain positions, potentially yielding incorrect parsing.<ref name="inimino" />
 
The five problematic tokens are the open parenthesis "<tt>(</tt>", open bracket "<tt>[</tt>", slash "<tt>/</tt>", plus "<tt>+</tt>", and minus "<tt>-</tt>". Of these, the open parenthesis is common in the [[immediately-invoked function expression]] pattern, and open bracket occurs sometimes, while others are quite rare. The example given in the spec<!-- The blog is not a spec[ification] --> is:<ref name="inimino" />
 
<syntaxhighlight lang="javascript">
Line 88:
 
==Variables==
[[Variable (programming)|Variable]]s in standard JavaScript have no [[Type system|type]] attached,<!-- Wait, variables do have a type, but you don't declare it. Also there's type coercion. --> and any value can be stored in any variable. Before ES6, variables were declared only with a <code>var</code> statement. Starting with [[ECMAScript#6th Edition - ECMAScript 2015|ES6]], the version of the language finalised in 2015, variables can also be declared with <code>let</code> (for a [[block scope|block level]] variable),or <code>varconst</code> (which are for a [[functionblock scope|functionblock level]] variable) or <code>const</code> (for an immutable one)variables. However, whileThe the objectvalue assigned to a <code>const</code> cannot be changed, but its properties can. BeforeAn ES6,[[Identifier variables were declared only with a <code>var</code> statement. An(computer languages)|identifier]] must start with a letter, underscore (<tt>_</tt>), or dollar sign (<tt>$</tt>); subsequent characters can also be digits (<tt>0-9</tt>). Because JavaScript is case sensitive, letters include the uppercase characters "A" through "Z" (uppercase)are anddifferent from the lowercase characters "a" through "z" (lowercase).
 
Starting with JavaScript 1.5, [[ISO 8859-1]] or [[Unicode]] letters (or <tt>\uXXXX</tt> Unicode escape sequences) can be used in identifiers.<ref>{{cite web | url=https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals&revision=22#Variables | title=Values, Variables, and Literals - MDC | date=16 September 2010 | publisher=Mozilla Developer Network | access-date=1 February 2020 | archive-url=https://web.archive.org/web/20110629131728/https://developer.mozilla.org/en/JavaScript/Guide/Values%2C_Variables%2C_and_Literals%26revision%3D22#Variables | archive-date=29 June 2011 | url-status=dead }}</ref> In certain JavaScript implementations, the at sign (@) can be used in an identifier, but this is contrary to the specifications and not supported in newer implementations.
Line 94:
===Scoping and hoisting===
 
Variables declared with <code>var</code> are [[lexical scoping|lexically scoped]] at a [[function scope|function level]], (notwhile [[blockones scope|blockwith level]]<code>let</code> asor in<code>const</code> C),have anda this does not depend on order ([[forwardblock declarationscope|block level]] isscope. notDeclarations necessary):are ifprocessed a variable is declared inside a function (atbefore any point,code inis any block), then inside the function, the name will resolve to that variableexecuted. This is equivalent in block scoping to variables being [[Forward declaration|forward declared]] at the top of the function or block, and is referred to as ''{{visible anchor|hoisting}}''.<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 -->
 
However,With <code>var</code> the variable value is <code>undefined</code> until it is initialized, and [[forward reference]] is not possible. 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 – only the declaration is hoisted, not the assignment. Variables declared with <code>let</code> or <code>const</code> do not set the value to <code>undefined</code>, so until it is initialized, referencing the variable 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 -->
 
Function statements, whose effect is to declare a variable of type <code>Function</code> and assign a value to it, 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.