Content deleted Content added
Lr |
m replaced: seperators → separators |
||
Line 1:
{{short description|Rule set defining a correctly structured JavaScript program}}▼
{{Multiple issues|
{{Very long|rps=79|date=May 2019}}
{{Update|date=November 2020|reason=New features/versions now in JavaScript}}
}}
▲{{short description|Rule set defining a correctly structured JavaScript program}}
{{Use dmy dates|date=April 2020}}
The '''[[Syntax (programming languages)|syntax]] of [[JavaScript]]''' is the set of rules that define a correctly structured JavaScript program.
Line 12:
==Origins==
[[Brendan Eich]] summarized the ancestry of the syntax in the first paragraph of the JavaScript 1.1 specification<ref>[http://hepunx.rl.ac.uk/~adye/jsspec11/intro.htm#1006028 JavaScript 1.1 specification]</ref><ref>{{
{{Quote|JavaScript borrows most of its syntax from [[Java (programming language)|Java]], but also inherits from [[Awk]] and [[Perl]], with some indirect influence from [[Self (programming language)|Self]] in its object prototype system.}}
Line 29:
===Whitespace and semicolons===
Unlike in [[C (programming language)|C]], whitespace in JavaScript source can directly impact [[Semantics (computer science)|semantics]]. [[Semicolon]]s end statements in JavaScript. Because of [[
|title=JavaScript: The definitive Guide
|url=https://archive.org/details/javascript00libg_297
Line 94:
[[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 variable. Starting with [[ECMAScript#6th Edition - ECMAScript 2015|ES6]], the 6th version of the language, 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. A variable's [[Identifier (computer languages)|identifier]] must start with a letter, underscore (<tt>_</tt>), or dollar sign (<tt>$</tt>), while subsequent characters can also be digits (<tt>0-9</tt>). 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 <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. {{
===Scoping and hoisting===
Line 230:
<syntaxhighlight lang="javascript">
// Note: Wikipedia syntax doesn't support numeric
1_000_000_000; // Used with big numbers
1_000_000.5; // Support with decimals
Line 297:
===BigInt===
BigInts can be used for arbitrarily large [[
When dividing BigInts, the results are [[Truncation|truncated]].
=== String ===
A [[String (computer science)|string]] in JavaScript is a sequence of characters. In JavaScript, strings can be created directly (as literals) by placing the series of characters between double (") or single (') quotes. Such strings must be written on a single line, but may include escaped newline characters (such as \n). The JavaScript standard allows the [[Grave accent#Use in programming|backquote]] character (`, a.k.a. grave accent or backtick) to quote multiline literal strings, but this is supported only on certain browsers as of 2016: Firefox and Chrome, but not Internet Explorer 11.<ref>{{
<syntaxhighlight lang="javascript">
Line 372:
===Boolean===
[[JavaScript]] provides a [[Boolean data type]] with {{mono|true}} and {{mono|false}} literals. The {{mono|[[typeof]]}} operator returns the string {{mono|"boolean"}} for these [[primitive types]]. When used in a logical context, {{mono|0}}, {{mono|-0}}, {{mono|null}}, {{mono|NaN}}, {{mono|undefined}}, and the empty string ({{mono|""}}) evaluate as {{mono|false}} due to automatic [[type coercion]]. All other values (the [[complement (set theory)|complement]] of the previous list) evaluate as {{mono|true}}, including the strings {{mono|"0"}}, {{mono|"false"}} and any object.
=== Type conversion ===
Line 1,517:
console.log(v); // Top
</syntaxhighlight>
===Async/await===
{{excerpt|Async/await|In JavaScript}}
Line 1,763 ⟶ 1,764:
===eval (expression) ===
Evaluates the first parameter as an expression, which can include assignment statements. Variables local to functions can be referenced by the expression. However, {{code|eval}} represents a major security risk, as it allows a bad actor to execute arbitrary code, so its use is discouraged.<ref name="deve_eval">{{
<syntaxhighlight lang="javascript">
(function foo() {
|