Math is a built-in object
that has properties and methods for mathematical constants and functions.
All properties and methods of
Math
are static.
You refer to the constant pi as
Math.PI
and you call the sine function as Math.sin(x)
,
where x
is the method's argument.
var
mygrade=Math.round(8.6) //returns 9
var mynum=Math.pow(2, 3) //returns 8
var mynum=Math.pow(2, 3) //returns 8
Properties
Properties
|
Description
|
E
|
The constant of E, the base of natural logarithms.
alert(Math.E);
// 2.718281828459045
|
LN2
|
The natural logarithm of 2.
alert(Math.LN2); // 0.6931471805599453
|
LN10
|
The natural logarithm of 10.
alert(Math.LN10); // 2.302585092994046
|
LOG2E
|
Base 2 logarithm of E.
alert(Math.LOG2E);//1.4426950408889634
|
LOG10E
|
Base 10 logarithm of E.
alert(Math.LOG10E);// 0.4342944819032518
|
PI
|
Returns PI.
alert(Math.PI);// 3.141592653589793
|
SQRT1_2
|
Square root of 1/2.
alert(Math.SQRT1_2);//0.7071067811865476
|
SQRT2
|
Square root of 2.
alert(Math.SQRT2);//1.4142135623730951
|
Methods
Methods
|
Description
|
abs(x)
|
Returns absolute value of x.
Example:
Math.abs('-1'); //
1
Math.abs(-2); // 2
Math.abs(null); //
0
Math.abs("string");
// NaN
Math.abs(); // NaN
Math.abs(‘1.234’); //1.234
Math.abs('-1.56') //1.56
|
acos(x)
|
Returns arc cosine of x in radians.
|
asin(x)
|
Returns arc sine of x in radians.
|
atan(x)
|
Returns arc tan of x in radians.
|
atan2(y, x)
|
Counter clockwise angle between x axis and point (x,y).
|
ceil(x)
|
Returns the smallest integer greater than or equal to x.
(round up).
Example:
alert(Math.ceil(7.0004));//8
alert(Math.ceil(7));//7
alert(Math.ceil(-7.01));//-7
alert(Math.ceil(-7.99));//-7
|
cos(x)
|
Returns cosine of x, where x is in radians.
|
exp(x)
|
Returns ex
|
floor(x)
|
Returns the largest integer less
than or equal to x. (round down)
Example:
alert(Math.floor(7.0004));//8
alert(Math.floor(7.99));//7
alert(Math.floor(-7.01));//-8
alert(Math.floor(-7.99));//-8
|
log(x)
|
Returns the natural logarithm (base
E) of x.
|
max(a, b)
|
Returns the larger of a and b.
Example:
alert(Math.max(12,12));//12
alert(Math.max(12,21));//21
|
min(a, b)
|
Returns the lesser of a and b.
Example:
alert(Math.min(12,12));//12
alert(Math.min(12,21));//12
|
pow(x, y)
|
Returns Xy
|
random()
|
Returns a pseudorandom number between 0 and 1.
Example:
Math.random();
//returns random number
between 0 and 1, such as 0.634343434...
Math.floor(Math.random()*11);
//returns random integer
between 0 and 10
|
round(x)
|
Rounds x up or down to the nearest integer. It rounds .5
up.
Example:
Math.round(25.9)
//returns 26
Math.round(25.2) //returns 25 Math.round(-2.58) //returns -3 var original=28.453 //round "original" to two decimals: Math.round(original*100)/100 //returns 28.45 |
sin(x)
|
Returns the Sin of x, where x is in radians.
|
sqrt(x)
|
Returns the square root of x.
|
tan(x)
|
Returns the Tan of x, where x is in radians.
|
No comments:
Post a Comment