JavaScript syntax: Difference between revisions

Content deleted Content added
BOT--Reverting link addition(s) by Coderlipi to revision 948179315 (www.youtube.com/watch?v=zBPeGR48_vE&list=PLqkLaKB2GJhWXV9rcarwvn06ISlL_9mPQ&index=1 [\byoutube\.com])
Line 1:
{{TooVery long|rps=79|date=May 2019}}{{JavaScriptSidebar}}
{{Use dmy dates|date=April 20112020}}
The '''[[Syntax (programming languages)|syntax]] of [[JavaScript]]''' is the set of rules that define a correctly structured JavaScript program.
 
Line 48 ⟶ 49:
with the suggestion that the preceding statement be terminated with a semicolon.
 
Some suggest instead the use of ''leading'' semicolons on lines starting with '<tt>(</tt>' or '<tt><nowiki>[</nowiki></tt>', so the line is not accidentally joined with the previous one. This is known as a '''defensive semicolon''', and is particularly recommended, because code may otherwise become ambiguous when it is rearranged.<ref name="inimino">"[http://inimino.org/~inimino/blog/javascript_semicolons JavaScript Semicolon Insertion: Everything you need to know]", [http://inimino.org/~inimino/blog/ ~inimino/blog/], Friday, 28 May 28, 2010</ref><ref>"[http://mislav.uniqpath.com/2010/05/semicolons/ Semicolons in JavaScript are optional]", by Mislav Marohnić, 077 May 2010</ref> For example:
 
<syntaxhighlight lang="javascript">
Line 87 ⟶ 88:
 
==Variables==
[[Variable (programming)|Variable]]s in standard JavaScript have no [[Type system|type]] attached, and any value can be stored in any variable. Starting with [[ECMAScript#6th_Edition_6th Edition -_ECMAScript_2015 ECMAScript 2015|ES6]], the version of the language finalised in 2015, variables can be declared with <code>let</code> (for a [[block scope|block level]] variable), <code>var</code> (for a [[function scope|function level]] variable) or <code>const</code> (for an immutable one). However, while the object assigned to a <code>const</code> cannot be changed, its properties can. Before ES6, variables were declared only with a <code>var</code> statement. An 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 characters "A" through "Z" (uppercase) and the 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 105 ⟶ 106:
</syntaxhighlight>
 
Block scoping can be produced by wrapping the entire block in a function and then executing it this is known as the [[immediately-invoked function expression]] pattern or by declaring the variable using the <code>let</code> keyword.
 
===Declaration and assignment===
Line 1,125 ⟶ 1,126:
|NOT
|-
| align="center" | <tt>&lt;&lt;</tt> || shift left (zero fill at right)
|-
| align="center" | <tt>&gt;&gt;</tt> || shift right (sign-propagating); copies of the<br />leftmost bit (sign bit) are shifted in from the left
Line 1,158 ⟶ 1,159:
| align="center" | <tt>^=</tt> || xor
|-
| align="center" | <tt>&lt;&lt;=</tt> || shift left (zero fill at right)
|-
| align="center" | <tt>&gt;&gt;=</tt> || shift right (sign-propagating); copies of the<br />leftmost bit (sign bit) are shifted in from the left
Line 1,659 ⟶ 1,660:
 
===eval (expression) ===
Evaluates expression string parameter, which can include assignment statements. Variables local to functions can be referenced by the expression. However, the {{code|eval}} represents a major security risk, as it allows a bad actor to execute arbitrary code, and so its use is discouraged.<ref name="deve_eval">{{Cite web |title=eval() |author= |work=MDN Web Docs |date= |access-date=29 January 2020 |url= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Never_use_eval!}}</ref>
<syntaxhighlight lang="javascript">
(function foo() {
Line 1,678 ⟶ 1,679:
* David Flanagan, Paula Ferguson: ''JavaScript: The Definitive Guide'', O'Reilly & Associates, {{ISBN|0-596-10199-6}}.
* Thomas A. Powell, Fritz Schneider: ''JavaScript: The Complete Reference'', McGraw-Hill Companies, {{ISBN|0-07-219127-9}}.
* Axel Rauschmayer: ''Speaking JavaScript: An In-Depth Guide for Programmers'', 460 pages, O'Reilly Media, February 25, February 2014, {{ISBN|978-1449365035}}. ([http://speakingjs.com/ free online edition])
* Emily Vander Veer: ''JavaScript For Dummies, 4th Edition'', Wiley, {{ISBN|0-7645-7659-3}}.
 
Line 1,693 ⟶ 1,694:
 
{{JavaScript}}
 
{{Use dmy dates|date=April 2011}}
 
{{DEFAULTSORT:Javascript Syntax}}