Javascript Math Method


Math Methods



ABS Method Back to top
Description

Returns the absolute value of a number.

Syntax

Math.abs(number)

The number argument is a numeric expression for which the absolute value is sought.

Remarks

The return value is the absolute value of the number argument.

The following example illustrates the use of the abs method:

function ComparePosNegVal(n)
{
  var s;
  var v1 = Math.abs(n);  
  var v2 = Math.abs(-n);
  if (v1 == v2)
    s = "The absolute values of " + n + " and "
    s += -n + " are identical.";
  return(s);
}


acos Method Back to top
Description

Returns the arccosine of a number.

Syntax

Math.acos(number)

The number argument is a numeric expression for which the arccosine is sought.
number
for acos must less or equal to 1.

Remarks

The return value is the arccosine of the number argument.


asin Method Back to top
Description

Returns the arcsine of a number.

Syntax

Math.asin(number)

The number argument is a numeric expression for which the arcsine is sought.
number for acos must less or equal to 1.

Remarks

The return value is the arcsine of its numeric argument.


atan Method Back to top
Description

Returns the arctangent of a number.

Syntax

Math.atan(number)

The number argument is a numeric expression for which the arctangent is sought.

Remarks

The return value is the arctangent of its numeric argument


atan2 Method Back to top
Description

Returns the angle (in radians) from the X axis to a point (y,x).

Syntax

Math.atan2(y, x)

The atan2 method syntax has these parts:

Part Description
Math Required. Invokes the intrinsic Math object.
x Required. A numeric expression representing the cartesian x-coordinate.
y Required. A numeric expression representing the cartesian y-coordinate.
Remarks

The return value is between -pi and pi, representing the angle of the supplied (y,x) point.


ceil Method Back to top
Description

Returns the smallest integer greater than or equal to its numeric argument.

Syntax

Math.ceil(number)

The number argument is a numeric expression.

Remarks

The return value is an integer value equal to the smallest integer greater than or equal to its numeric argument.


cos Method Back to top
Description

Returns the cosine of a number.

Syntax

Math.cos(number)

The number argument is a numeric expression for which the cosine is sought.

Remarks

The return value is the cosine of its numeric argument.


exp Method Back to top
Description

Returns e (the base of natural logarithms) raised to a power.

Syntax

Math.exp(number)

The number argument is a numeric expression representing the power of e.

Remarks

The return value is enumber. The constant e is Euler's constant, approximately equal to 2.178 and number is the supplied argument.


floor Method Back to top
Description

Returns the greatest integer less than or equal to its numeric argument.

Syntax

Math.floor(number)

The number argument is a numeric expression.

Remarks

The return value is an integer value equal to the greatest integer less than or equal to its numeric argument.


log Method Back to top

Description

Returns the natural logarithm of a number.

Syntax

Math.log(number)

The number argument is a numeric expression for which the natural logarithm is sought.
number must be larger than 0!

Return Value

The return value is the natural logarithm of number. The base is e.


max Method Back to top
Description

Returns the greater of two supplied numeric expressions.

Syntax

retVal = Math.max(number1, number2)

The max method syntax has these parts:

Part Description
retVal The greater of number1 or number2.
number1 A numeric expression to be compared to number2.
number2 A numeric value to be compared to number1.

min Method Back to top
Description

Returns the lesser of two supplied numbers.

Syntax

retVal = Math.min(number1, number2)

The min method syntax has these parts:

Part Description
retVal The lesser of number1 or number2.
number1 A numeric expression to be compared to number2.
number2 A numeric value to be compared to number1.

pow Method Back to top

Description

Returns the value of a base expression taken to a specified power.

Syntax

Math.pow(base, exponent)

The pow method syntax has these parts:

Part Description
base The base value of the expression.
exponent The exponent value of the expression.
Remarks

In the following example, a numeric expression equal to baseexponent returns 1000.

Math.pow(10,3);

random Method Back to top

Description

Returns a pseudorandom number between 0 and 1.

Syntax

Math.random( )

Remarks

The pseudorandom number generated is between 0 and 1 inclusive. The random number generator is seeded automatically when JScript is first loaded.
Click 2 or more times to see the result


round Method Back to top

Description

Returns a supplied numeric expression rounded to the nearest integer.

Syntax

Math.round(number)

The number argument is the value to be rounded to the nearest integer.

Remarks

If the decimal portion of number is 0.5 or greater, the return value is equal to the smallest integer greater than number. Otherwise, round returns the largest integer less than or equal to number.



sin Method Back to top

Description

Returns the sine of a number.

Syntax

Math.sin(number)

The number argument is a numeric expression for which the sine is sought.

Remarks

The return value is the sine of its numeric argument


sqrt Method Back to top
Description

Returns the square root of a number.

Syntax

Math.sqrt(number)

The number argument is a numeric expression.

Remarks

If number is negative, the return value is zero



tan Method Back to top

Description

Returns the tangent of a number.

Syntax

Math.tan(number)

The number argument is a numeric expression for which the tangent is sought.

Remarks

The return value is the tangent of number.


E Property

Description

Returns Euler's constant, the base of natural logarithms. The E property is approximately equal to 2.718.

Syntax

var numVar
numVar
= Math.E


LN2 Property

Description

Returns the natural logarithm of 2.

Syntax

var numVar
numVar
= Math.LN2

Syntax

The LN2 property is approximately equal to 0.693.


LN10 Property

Description

Returns the natural logarithm of 10.

Syntax

var numVar
numVar
= Math.LN10

Remarks

The LN10 property is approximately equal to 2.302.


LOG2E Property

Description

Returns the base-2 logarithm of e, Euler's constant.

Syntax

var varName
varName = objName
.LOG2E

Remarks

The LOG2E property, a constant, is approximately equal to 1.442.


LOG10E Property

Description

Returns the base-10 logarithm of e, Euler's constant.

Syntax

var varName
varName = objName
.LOG10E

Remarks

The LOG10E property, a constant, is approximately equal to 0.434


PI Property

Description

Returns the ratio of the circumference of a circle to its diameter, approximately 3.141592653589793.

Syntax

var numVar
numVar
= Math.PI

Syntax

The PI property, a constant, is approximately equal to 3.14159


SQRT1_2 Property

Description

Returns he square root of 0.5, or one divided by the square root of 2.

Syntax

var numVar
numVar
= Math.SQRT1_2

Remarks

The SQRT1_2 property, a constant, is approximately equal to 0.707.


SQRT2 Property

Description

Returns the square root of 2.

Syntax

var numVar
numVar
= Math.SQRT2

Syntax

The SQRT2 property, a constant, is approximately equal to 1.414