JavaScript syntax: Difference between revisions

Content deleted Content added
If … else: Unnecessary reexplanation provided, thus removed
No edit summary
Tag: nowiki added
Line 84:
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 ''{{visible anchor|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>|lang=javascript|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>|lang=javascript|code=x = 1</code>}} assignment statement at that point in the middle of the function – only the declaration is hoisted, not the assignment.
 
Function statements, whose effect is to declare a variable of type <code>Function</code> and assign a value to it, are similar to variable statements, but in addition to hoisting the declaration, they also hoist the assignment – as if the entire statement appeared at the top of the containing function – and thus forward reference is also possible: the ___location of a function statement within an enclosing function is irrelevant.
Line 450:
 
===Math===
The <tt>Math</tt> object contains various math-related constants (for example, π{{pi}}) and functions (for example, cosine). (Note that the <tt>Math</tt> object has no constructor, unlike <tt>Array</tt> or <tt>Date</tt>. All its methods are "static", that is "class" methods.) All the trigonometric functions use angles expressed in [[radian]]s, not [[Degree (angle)|degrees]] or [[Grad (angle)|grads]].
 
{| class="wikitable" border="1"
|+ Properties of the Math object
|-
!Property!!Returned value<br />rounded to 5 digits!!Description
|-
| <tt>Math.E</tt> || align=center|2.7183 || ''[[e (mathematical constant)|e]]'': Natural logarithm base
Line 467:
| <tt>Math.LOG10E</tt> || align=center|0.43429 || Logarithm to the base 10 of ''e''
|-
| <tt>Math.PI</tt> || align=center|3.14159 || [[Pi|π{{pi}}]]: circumference/diameter of a circle
|-
| <tt>Math.SQRT1_2</tt> || align=center|0.70711 || [[Square root]] of ½
Line 477:
|+ Methods of the Math object
|-
!Example!!Returned value<br />rounded to 5 digits!!Description
|-
| <tt>Math.abs(-2.3)</tt> || align=center|2.3 || Absolute value: <tt>(x < 0) ? -x : x</tt>
Line 485:
| <tt>Math.asin(Math.SQRT1_2)</tt> || align=center|0.78540 rad. = 45° || [[Arcsine]]
|-
| <tt>Math.atan(1)</tt> || align=center|0.78540 rad. = 45° || Half circle [[arctangent]] (-π{{pi}}/2 to +π{{pi}}/2)
|-
| <tt>Math.atan2(-3.7, -3.7)</tt> || align=center|-2.3562 rad. = -135° || Whole circle arctangent (-π{{pi}} to +π{{pi}})
|-
| <tt>Math.ceil(1.1)</tt> || align=center|2 || Ceiling: [[rounding|round]] up to smallest integer ≥ argument
Line 938:
| align="center" | <tt>&lt;&lt;</tt> || shift left (zero fill)
|-
| align="center" | <tt>&gt;&gt;</tt> || shift right (sign-propagating); copies of the<br />leftmost bit (sign bit) are shifted in from the left
|-
| align="center" | <tt>&gt;&gt;&gt;</tt> || shift right (zero fill). For positive numbers,<br /><tt>&gt;&gt;</tt> and <tt>&gt;&gt;&gt;</tt> yield the same result.
|}
 
Line 1,061:
* Iterates through all enumerable properties of an object.
* Iterates through all used indices of array including all user-defined properties of array object, if any. Thus it may be better to use a traditional for loop with a numeric index when iterating over arrays.
* There are differences between the various Web browsers with regard to which properties will be reflected with the for...in loop statement. In theory, this is controlled by an internal state property defined by the ECMAscript standard called "DontEnum", but in practice, each browser returns a slightly different set of properties during introspection. It is useful to test for a given property using <{{code>|lang=javascript|code=if (some_object.hasOwnProperty(property_name)) { ... }<nowiki /code>}}. Thus, adding a method to the array prototype with <{{code>|lang=javascript|code=Array.prototype.newMethod = function() {...}<nowiki /code>}} may cause <code>for … in</code> loops to loop over the method's name.
 
===While loop===
Line 1,150:
Functions are [[first class object]]s and may be assigned to other variables.
 
The number of arguments given when calling a function may not necessarily correspond to the number of arguments in the function definition; a named argument in the definition that does not have a matching argument in the call will have the value {{mono|undefined}} (that can be implicitly cast to false). Within the function, the arguments may also be accessed through the {{mono|arguments}} object; this provides access to all arguments using indices (e.g. <{{code>|lang=javascript|code=arguments[0], arguments[1], ... arguments[n]</code>}}), including those beyond the number of named arguments. (While the arguments list has a <tt>.length</tt> property, it is ''not'' an instance of {{mono|Array}}; it does not have methods such as {{mono|.slice()}}, {{mono|.sort()}}, etc.)
 
<syntaxhighlight lang=JavaScript>