JavaScript syntax: Difference between revisions

Content deleted Content added
Scoping and hoisting: Clarified variable use (capabilities) with JS hoisting.
m Math: {{mono}}, {{val}}
Line 601:
 
===Math===
The <tt>{{mono|Math</tt>}} object contains various math-related constants (for example, {{pi}}) and functions (for example, cosine). (Note that the <tt>{{mono|Math</tt>}} object has no constructor, unlike <tt>{{mono|Array</tt>}} or <tt>{{mono|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"
Line 608:
!Property!!Returned value<br />rounded to 5 digits!!Description
|-
| <tt>{{mono|Math.E</tt>}} || align=center|2.7183 || {{mvar|[[e (mathematical constant)|e]]}}: Natural logarithm base
|-
| <tt>{{mono|Math.LN2</tt>}} || align=center|0.69315 || [[Natural logarithm]] of 2
|-
| <tt>{{mono|Math.LN10</tt>}} || align=center|2.3026 || Natural logarithm of 10
|-
| <tt>{{mono|Math.LOG2E</tt>}} || align=center|1.4427 || [[Logarithm]] to the base 2 of {{mvar|e}}
|-
| <tt>{{mono|Math.LOG10E</tt>}} || align=center|0.43429 || Logarithm to the base 10 of {{mvar|e}}
|-
| <tt>{{mono|Math.PI</tt>}} || align=center|3.14159 || [[Pi|{{pi}}]]: circumference/diameter of a circle
|-
| <tt>{{mono|Math.SQRT1_2</tt>}} || align=center|0.70711 || [[Square root]] of ½
|-
| <tt>{{mono|Math.SQRT2</tt>}} || align=center|1.4142 || [[Square root of 2]]
|}
 
Line 630:
!Example!!Returned value<br />rounded to 5 digits!!Description
|-
| <tt>{{mono|Math.abs(-2.3)</tt>}} || align=center|2.3 || [[Absolute value]]
|-
| <tt>{{mono|Math.acos(Math.SQRT1_2)</tt>}} || align=center{{val|0.78540 |ul=rad.|fmt=none}} = 45° || [[Arccosine]]
|-
| <tt>{{mono|Math.asin(Math.SQRT1_2)</tt>}} || align=center{{val|0.78540 |u=rad.|fmt=none}} = 45° || [[Arcsine]]
|-
| <tt>{{mono|Math.atan(1)</tt>}} || align=center{{val|0.78540 |u=rad.|fmt=none}} = 45° || Half circle [[arctangent]] (-{{tmath|-\pi}}/2}} to +{{tmath|+\pi}}/2}})
|-
| <tt>{{mono|Math.atan2(-3.7, -3.7)</tt>}} || align=center{{val|-2.3562 |u=rad.}} = {{val|-135°|u=deg}} || Whole circle arctangent (-{{tmath|-\pi}} to +{{tmath|+\pi}})
|-
| <tt>{{mono|Math.ceil(1.1)</tt>}} || align=center|2 || Ceiling: [[rounding|round]] up to smallest integer ≥ argument
|-
| <tt>{{mono|Math.cos(Math.PI/4)</tt><tt></tt>}} || align=center|0.70711 || [[Trigonometric functions|Cosine]]
|-
| <tt>{{mono|Math.exp</tt>}}(1) || align=center|2.7183 || [[Exponential function]]: {{mvar|e}} raised to this power
|-
| <tt>{{mono|Math.floor(1.9)</tt>}} || align=center|1 || Floor: round down to largest integer ≤ argument
|-
| <tt>{{mono|Math.log(Math.E)</tt>}} || align=center|1 || Natural logarithm, base {{mvar|e}}
|-
| <tt>{{mono|Math.max(1, -2)</tt>}} || align=center|1 || Maximum: <tt>{{mono|(x > y) ? x : y</tt>}}
|-
| <tt>{{mono|Math.min(1, -2)</tt>}} || align=center{{val|-2}} || Minimum: <tt>{{mono|(x < y) ? x : y</tt>}}
|-
| <tt>{{mono|Math.pow(-3, 2)</tt>}} || align=center|9 || [[Exponentiation]] (raised to the power of): <tt>{{mono|Math.pow(x, y)</tt>}} gives x<sup>y</sup>
|-
| <tt>{{mono|Math.random()</tt>}} || align=center|e.g. 0.17068 || [[Pseudorandom]] number between 0 (inclusive) and 1 (exclusive)
|-
| <tt>{{mono|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>{{mono|Math.sin(Math.PI/4)</tt>}} || align=center|0.70711 || [[Sine]]
|-
| <tt>{{mono|Math.sqrt(49)</tt>}} || align=center|7 || Square root
|-
| <tt>{{mono|Math.tan(Math.PI/4)</tt>}} || align=center|1 || [[Trigonometric functions|Tangent]]
|}