JavaScript: Difference between revisions

Content deleted Content added
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 = JavaScript
| logo = [[File:JavaScript-logo.png|thumb|Commonly used Logo]]
| logo alt =
| paradigm = [[Multi-paradigm]]: [[object-oriented programming|object-oriented]] ([[prototype-based programming|prototype-based]]), [[imperative programming|imperative]], [[functional programming|functional]], [[event-driven programming|event-driven]]{{Sfn|Flanagan|2011|pp =1–2}}
| designer = [[Brendan Eich]]
| developer = [[Netscape|Netscape Communications Corporation]], [[Mozilla Foundation]], [[Ecma International]]
| typing = [[Dynamic typing|Dynamic]], [[Duck typing|duck]]
| implementations typing = [[Dynamic typing|Dynamic]], [[Duck typing|duck]]
| implementations = [[V8 (JavaScript engine)|V8]], [[JavaScriptCore]], [[SpiderMonkey (JavaScript engine)|SpiderMonkey]], [[Chakra (JScript engine)|Chakra]]
| influenced = [[ActionScript]], [[AtScript]], [[CoffeeScript]], [[Dart (programming language)|Dart]], [[JScript .NET]], [[LiveScript]], [[Objective-J]], [[Opa (programming language)|Opa]], [[Perl 6]], [[QML]], [[TypeScript]]
| license =
| File extensions = <code>.js</code>
| File format =
| website = [https://developer.mozilla.org/en-US/docs/Web/JavaScript Mozilla]
| wikibooks = JavaScript
| year = {{start date and age|1995|12|04}}<ref name="press_release">[https://web.archive.org/web/20070916144913/http://wp.netscape.com/newsref/pr/newsrelease67.html Press release announcing JavaScript], "Netscape and Sun announce JavaScript", PR Newswire, December 4, 1995</ref>
| 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 = {{start date and age|2017|6}}
| latest_preview_version =
| latest_preview_date =
| influenced_by = [[Lua (programming language)|Lua]], [[Scheme (programming language)|Scheme]], [[Perl]], [[Self (programming language)|Self]], [[Java (programming language)|Java]], [[C (programming language)|C]], [[Python (programming language)|Python]], [[AWK]], [[HyperTalk]] <!--True for first JavaScript? Or only ECMAScript – later JavaScript versions-->
}}
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 <ttcode>var</ttcode> 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>
 
<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>