Content deleted Content added
Tag: Reverted |
ToadetteEdit (talk | contribs) Reverted 8 edits by 111.88.34.155 (talk): Too much spamming |
||
Line 13:
==Origins==
[[Brendan Eich]] summarized the ancestry of the
{{Quote|
==Basics==
===Case sensitivity===
Example:
Line 28:
console.log(A); // throws a ReferenceError: A is not defined
</syntaxhighlight>
===Whitespace and semicolons===
Line 54 ⟶ 52:
// a = b + c(d + e).foo();
</syntaxhighlight>
with the suggestion that the preceding statement be terminated with a semicolon.
Line 68 ⟶ 64:
// (d + e).foo();
</syntaxhighlight>
Initial semicolons are also sometimes used at the start of JavaScript libraries, in case they are appended to another library that omits a trailing semicolon, as this can result in ambiguity of the initial statement.
Line 84 ⟶ 78:
// return a + b;
</syntaxhighlight>
===Comments===
{{Main|Comment (computer programming)}}
[[Comment (computer programming)|Comment]] syntax is the same as in
<syntaxhighlight lang="javascript">
Line 99 ⟶ 92:
/* Comments /* may not be nested */ Syntax error */
</syntaxhighlight>
==Variables==
{{Main|Variable (programming)}}
[[Variable (programming)|Variable]]s in standard JavaScript have no [[Type system|type]] attached, so any value (each ''value'' has a type) can be stored in any
Starting with JavaScript 1.5, [[ISO 8859-1]] or [[Unicode]] letters (or <code>\uXXXX</code> 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. {{Citation needed|date=January 2021}}
Line 121 ⟶ 113:
function func() { .. } // declaration and assignment are hoisted
</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. <!-- Two totally slightly different things, wrapping cope in a block executes the whole code in a block, while let-or-const variables can be seen in the same block. So block-scope code, where code is executed, block-scope variables, where variables can be accessed. -->
Line 163 ⟶ 154:
console.log(z); // This line will raise a ReferenceError exception, because the value of z is no longer available
</syntaxhighlight>
<syntaxhighlight lang="javascript">
for (let i = 0; i < 10; i++) console.log(i);
console.log(i); // throws a ReferenceError: i is not defined
</syntaxhighlight>
<syntaxhighlight lang="javascript">
for (const i = 0; i < 10; i++) console.log(i); // throws a TypeError: Assignment to constant variable
Line 176 ⟶ 167:
const pi; // throws a SyntaxError: Missing initializer in const declaration
</syntaxhighlight>
==Primitive data types==
Line 209 ⟶ 199:
console.log(undefined === null); // enforce type during check, displays false
</syntaxhighlight>
Note: There is no built-in language literal for undefined. Thus {{code|lang=javascript|code=(x === undefined)}} is not a foolproof way to check whether a variable is undefined, because in versions before ECMAScript 5, it is legal for someone to write {{code|lang=javascript|code=var undefined = "I'm defined now";}}. A more robust approach is to compare using {{code|lang=javascript|code=(typeof x === 'undefined')}}.
Line 220 ⟶ 209:
function isUndefined(x) { return (typeof x) === "undefined"; } // ... or that third one
</syntaxhighlight>
Here, calling <code>isUndefined(my_var)</code> raises a {{mono|ReferenceError}} if {{mono|my_var}} is an unknown identifier, whereas {{code|lang=javascript|code=typeof my_var === 'undefined'}} doesn't.
Line 1,829 ⟶ 1,817:
* Mozilla Developer Center Core References for JavaScript versions [https://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference 1.5], [https://web.archive.org/web/20070210000908/http://research.nihonsoft.org/javascript/CoreReferenceJS14/index.html 1.4], [https://web.archive.org/web/20070210000504/http://research.nihonsoft.org/javascript/ClientReferenceJS13/index.html 1.3] and [https://web.archive.org/web/20070210000545/http://research.nihonsoft.org/javascript/jsref/index.htm 1.2]
* [https://developer.mozilla.org/en/docs/JavaScript Mozilla JavaScript Language Documentation]
{{JavaScript}}
|