Content deleted Content added
Tag: Reverted |
Tags: Reverted possible vandalism |
||
Line 104:
{{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 [https://jsonlinecompiler.com/javascript/javascript-variables/ variable]. Starting with [[ECMAScript#6th Edition – ECMAScript 2015|ES6]], the 6th version of the language, [https://jsonlinecompiler.com/javascript/javascript-variables/ variables] could be declared with <code>var</code> for function scoped variables, and <code>let</code> or <code>const</code> which are for [[block scope|block level]] variables. Before ES6, variables could only be declared with a <code>var</code> statement. Values assigned to variables declared with <code>const</code> cannot be changed, but its properties can. <code>var</code> should no longer be used since <code>let</code> and <code>const</code> are supported by modern browsers.<ref>{{Cite web |date=2023-05-09 |title=Storing the information you need — Variables - Learn web development {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables |access-date=2023-06-23 |website=developer.mozilla.org |language=en-US}}</ref> A variable's [[Identifier (computer languages)|identifier]] must start with a letter, underscore (<code>_</code>), or dollar sign (<code>$</code>), while subsequent characters can also be digits (<code>0-9</code>). JavaScript is case sensitive, so the uppercase characters "A" through "Z" are different from the lowercase characters "a" through "z".
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:
function func() { .. } // declaration and assignment are hoisted
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
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 162 ⟶ 163:
console.log(z); // This line will raise a ReferenceError exception, because the value of z is no longer available
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
<syntaxhighlight lang="javascript">
for (let i = 0; i < 10; i++) console.log(i);
console.log(i); // throws a ReferenceError: i is not defined
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
<syntaxhighlight lang="javascript">
for (const i = 0; i < 10; i++) console.log(i); // throws a TypeError: Assignment to constant variable
Line 175 ⟶ 176:
const pi; // throws a SyntaxError: Missing initializer in const declaration
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
==Primitive data types==
|