JavaScript syntax: Difference between revisions

Content deleted Content added
Reverted 1 good faith edit by 193.206.251.4 using STiki
clean up, typo(s) fixed: Furthermore → Furthermore, using AWB
Line 3:
The '''[[Syntax (programming languages)|syntax]] of [[JavaScript]]''' is the set of rules that define a correctly structured JavaScript program.
 
The examples below make use of the alert function for [[Standard streams#Standard_output_Standard output .28stdout.29|standard text output]]. The JavaScript [[standard library]] lacks an official standard text output function. However, given that JavaScript is mainly used for [[client-side scripting]] within modern [[Web browser]]s, and that almost all Web browsers provide the alert function, <tt>alert</tt> is used in the examples.
 
==Origins==
Line 76:
 
==Variables==
[[Variable (programming)|Variable]]s in standard JavaScript have no [[Type system|type]] attached, and any value can be stored in any variable. Variables are declared with a <code>var</code> statement, multiple variables can be declared at once. An identifier must start with a letter, underscore (<tt>_</tt>), or dollar sign (<tt>$</tt>); subsequent characters can also be digits (<tt>0-9</tt>). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
 
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}}</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===
 
Variables are [[lexical scoping|lexically scoped]] at [[function scope|function level]] (not [[block scope|block level]] as in C), and this does not depend on order ([[forward declaration]] is not necessary): if a variable is declared inside a function (at any point, in any block), then inside the function, the name will resolve to that variable. This is equivalent in block scoping to variables being forward declared at the top of the function, and is referred to as ''hoisting''.<ref>"[http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html JavaScript Scoping and Hoisting]", [http://www.adequatelygood.com/about.html Ben Cherry], ''[http://www.adequatelygood.com/ Adequately Good],'' 2010-02-08</ref>
 
However, the variable value is <code>undefined</code> until it is initialized, and [[forward reference]] is not possible. Thus a <code>var x = 1</code> statement in the middle of the function is equivalent to a <code>var x</code> declaration statement at the top of the function, and an <code>x = 1</code> assignment statement at that point in the middle of the function – only the declaration is hoisted, not the assignment.
Line 491:
| <tt>Math.ceil(1.1)</tt> || align=center|2 || Ceiling: [[rounding|round]] up to smallest integer ≥ argument
|-
| <tt>Math.cos(Math.PI/4)</tt><tt></tt> || align=center|0.70711 || [[Trigonometric_functionsTrigonometric functions#cosine|Cosine]]
|-
| <tt>Math.exp</tt>(1) || align=center|2.7183 || [[Exponential function]]: ''e'' raised to this power
Line 509:
| <tt>Math.round(1.5)</tt> || align=center|2 || Round to the nearest integer; half fractions are rounded up (e.g. 1.5 rounds to 2)
|-
| <tt>Math.sin(Math.PI/4)</tt> || align=center|0.70711 || [[Trigonometric_functionsTrigonometric functions#sine|Sine]]
|-
| <tt>Math.sqrt(49)</tt> || align=center|7 || Square root
Line 926:
===Bitwise===
{{Expand section|date=April 2011}}
JavaScript supports the following '''binary [[Bitwise_operationBitwise operation|bitwise operators]]''':
 
{| class="wikitable"
Line 1,145:
==Functions==
 
A [[function (programming)|function]] is a block with a (possibly empty) parameter list that is normally given a name. A function may use local variables. If you exit the function without a return statement, the value {{mono|undefined}} is returned.
 
<syntaxhighlight lang=JavaScript>
Line 1,188:
</syntaxhighlight>
 
Functions can be declared inside other functions, and access the outer function's local variables. Furthermore, they implement full [[closure (computer science)|closure]]s by remembering the outer function's local variables even after the outer function has exited.
 
<syntaxhighlight lang=JavaScript>