JavaScript » Objects » Math

The Math object is a top-level, built-in JavaScript object which can be accessed without using a constructor or calling a method. It also has static properties and methods for mathematical constants and functions. This means that you can refer to, say, the constant PI as Math.PI, and you can call the Tangent function with Math.tan(x). All constants are defined as precision real numbers in JavaScript.

Examples

Code:
len = Math.tan(theta) * adj
Explanation:

To illustrate this how to call the static methods of Math, this example calculates the length of the side of a right-angled triangle opposite the angle Theta.

Code:
with(Math)
{
   a = 28.27
   adj = sqrt(a/PI)
   len = adj * tan(1.1071)
}
Explanation:

When using several Math constants and methods, it is often more convenient to use the with statement to avoid having to repeatedly type the word Math.

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

E

Syntax: Math.E

This property is the base of natural logarithms (e, approximately 2.7183)

LN10

Syntax: Math.LN10

This property is the natural logarithm of 10, (approximately 2.3026).

LN2

Syntax: Math.LN2

This property is the natural logarithm of 2, which is approximately 0.6931.

LOG10E

Syntax: Math.LOG10E

This property is the base 10 logarithm of E (approximately 0.4343).

LOG2E

Syntax: Math.LOG2E

This property is the base 2 logarithm of E (approximately 1.4427).

PI

Syntax: Math.PI

This property is the ratio of the circuference of a circle to its diameter (approximately 3.1416).

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

SQRT1_2

Syntax: Math.SQRT1_2

This property is the value of 1 divided by the square root of 2 and is approximately equal to 0.7071.

SQRT2

Syntax: Math.SQRT2

This property is the square root of 2 (approximately 1.4142).

Methods

abs

Syntax: Math.abs(x)

This method returns the absolute value of a number.

acos

Syntax: Math.acos(x)

This method returns the arccosine of a number as a numeric value between 0 and PI radians. Passing it a value for 'x' which is outsite the range -1 to 1 will cause the Netscape browser to return NaN, and the Internet Explorer browser to return an error message. Passing it -1 will return the value of PI.

asin

Syntax: Math.asin(x)

This method returns the arcsine of a number as a numeric value between
-PI/2 and PI/2 radians. Passing it a value for 'x' which is outsite the range -1 to 1 will cause the Netscape browser to return NaN, and the Internet Explorer browser to return an error message. Passing it 1 will return the value of PI/2.

atan

Syntax: Math.atan(x)
 
atan2 Method
This method returns the arctangent of the quotient of its arguments.
 
Syntax: Math.atan2(y, x)

This method returns the arctangent of a number as a numeric value between -PI/2 and PI/2 radians.

ceil

Syntax: Math.ceil(x)

This method returns an integer equal to, or the next integer greater than, the number passed to it. Hence, if you passed it 3.79, it would return 4, and passing it -3.79 would return -3.

cos

Syntax: Math.cos(x)

This method returns the cosine of a number, which will be a numeric value between -1 and 1.

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

exp

Syntax: Math.exp(x)

This method returns the value of Ex where E is Euler's constant and x is the argument passed to it.

floor

Syntax: Math.floor(x)

This method returns an integer equal to, or the next integer less than, the number passed to it. Hence, if you passed it 3.79, it would return 3, and passing it -3.79 would return -4.

log

Syntax: Math.log(x)

This method returns the natural logarithm (base E) of a number. If you pass the log method the number 0, the Netscape browser will return -Infinity, and with an argument of a negative number NaN. In both these cases Internet Explorer returns an error message.

max

Syntax: Math.max(x, y)

This method returns the greater of the two numbers passed to it as arguments. Hence, if you passed it the numbers 9 and 11, it would return 11, whereas passing it -9 and -11 returns -9.

min

Syntax: Math.min(x, y)

This method returns the lesser of the two numbers passed to it as arguments. Hence, if you passed it the numbers 9 and 11, it would return 9, whereas passing it -9 and -11 returns -11.

pow

Syntax: Math.pow(x, y)

This method returns the value of x to the power of y (xy), where x is the base, and y is the exponent.

random

Syntax: Math.random()

This method takes no arguments and returns a pseudo-random number between 0 and 1. The random number generator is seeded from the current time.

round

Syntax: Math.round(x)

This method is used to round a number to the nearest integer. If the fractional portion of the number is .5 or higher, then the number is rounded up, otherwise it is rounded down.

sin

Syntax: Math.sin(x)

This method is used to return the sine of its argument, which will be a number between -1 and 1.

sqrt

Syntax: Math.sqrt(x)

This method returns the square root of a number. If that number is negative, then the Netscape browser returns the value of NaN, whereas the Internet Explorer browser returns an Error message.

tan

Syntax: Math.tan(x)

This method returns a number representing the tangent of an angle.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: