Content deleted Content added
KylieTastic (talk | contribs) m Reverted 1 edit by 119.160.97.121 (talk) to last revision by PackMecEng. (TW) |
Added logo and explained difference between var and let / const |
||
Line 2:
{{Infobox programming language
| name
| logo = [[File:JavaScript-logo.png|thumb|Commonly used Logo]]
| logo alt
| paradigm
| designer
| developer
|
| implementations = [[V8 (JavaScript engine)|V8]], [[JavaScriptCore]], [[SpiderMonkey (JavaScript engine)|SpiderMonkey]], [[Chakra (JScript engine)|Chakra]] | influenced
| license
| File extensions
| File format
| website
| wikibooks
| year
| latest_release_version = ECMAScript 2017<ref name="auto">{{cite web|url=https://www.ecma-international.org/publications/standards/Ecma-262.htm|title=Standard ECMA-262|publisher=Ecma International|date=2017-07-03}}</ref>
| latest_release_date
| latest_preview_version =
| latest_preview_date
| influenced_by
}}
Line 185 ⟶ 186:
=== Simple examples ===
[[Variable (computer science)|Variables]] in JavaScript can be defined using the <tt>var</tt> keyword:<ref>{{cite web | url=https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var | title=var – JavaScript – MDN | publisher=The [[Mozilla Developer Network]] | accessdate=22 December 2012}}</ref>▼
==== Variable declaration ====
▲[[Variable (computer science)|Variables]] in JavaScript can be defined using the <
<syntaxhighlight lang="javascript">
Line 194 ⟶ 197:
Note the [[Comment (computer programming)|comments]] in the example above, all of which were preceded with two [[Slash (punctuation)|forward slashes]].
In ECMAScript 2016 <code>let</code> and <code>const</code> were introduced as block level variable definitions. <code>let</code> works just like <code>var</code> but will expire after the block ends. <code>const</code> can only be assigned a value when declaring it.<ref>{{Cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let|title=let|website=MDN Web Docs|language=en-US|access-date=2018-03-25}}</ref><syntaxhighlight lang="javascript">
let changeableVariable = 'I am a let'; // defines changeable variable just like var
const constantVariable = 'I am a const'; // defines a constant variabe which value can only be set at the declaration
changeableVariable = 'I can be changed.'; // Will change value of 'changeableVariable'
constantVariable = 'I cannot be changed!!'; // Will throw 'TypeError' for reassigning a constant
</syntaxhighlight>
===== Difference between <code>var</code> and <code>let</code> /<code>const</code> =====
<syntaxhighlight lang="javascript">
let letOutsideBlock = 'letOutsideBlock'; // defines a variable which is only accessable in the block that it has been defined in
const constOutsideBlock = 'constOutsideBlock'; // defines a constant which is only accessable in the block that it has been defined in
var varOutsideBlock = 'varOutsideBlock'; // defines variable which is accessable in the entire function scope, even if defined in a block
if(true) { // Block which will always run for demonstration
let letInBlock = 'letInBlock'; // Will expire after the block ends
const constInBlock = 'constInBlock'; // Will expire after the block ends
var varInBlock = 'varInBlock'; // Will remain after the block ends
// Printing of values to the console, these will all work:
console.log('-- Inside Block:')
console.log(letOutsideBlock);
console.log(letInBlock);
console.log(constOutsideBlock);
console.log(constInBlock);
console.log(varOutsideBlock);
console.log(varInBlock);
}
// Printing of values to the console:
console.log('-- Outside Block:')
console.log(letOutsideBlock); // will run
console.log(constOutsideBlock); // will run
console.log(varOutsideBlock); // will run
console.log(varInBlock); // will run
console.log(letInBlock); // will throw 'ReferenceError' as it is no longer defined
console.log(constInBlock); // will throw 'ReferenceError' as it is no longer defined
</syntaxhighlight>
There is no built-in [[Input/output|I/O]] functionality in JavaScript; the run-time environment provides that. The ECMAScript specification in edition 5.1 mentions:<ref>{{cite web | url=http://www.ecma-international.org/ecma-262/5.1/#sec-4 | title=ECMAScript Language Specification – ECMA-262 Edition 5.1|publisher=[[Ecma International]] | accessdate=22 December 2012}}</ref>
|