JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Math

Math  static object

A static class containing mathematical constants and methods.

Description

A suite of useful mathematical functions for use in our web pages.

  • The Math global object is a static object which means we cannot instantiate objects of type Math.
  • All the properties and methods of this class are also static and so we access these using Math followed by the name of the property or method we wish to use.

Syntax


Signature Description
Math.classPropertyNameUsing the Math static object with a class property.
Math.classMethodName()Using the Math static object with a class method.

Parameters


Parameter Description
classPropertyNameThe name of the class property we wish to use.
classMethodName()The name of the class method we wish to use.

Class Properties


Class Property Description
EEuler's constant.
LN2Natural logarithm of 2.
LN10Natural logarithm of 10.
LOG2EBase 2 logarithm of E.
LOG10EBase 10 logarithm of E.
PICircumference to diameter ratio of a circle.
SQRT1_2Square root of 1/2.
SQRT2Square root of 2.

Instance Properties

We cannot instantiate Math objects and so there are no instance properties.

Class Methods


Class Method Description
abs()Returns the absolute value of a number.
acos()Returns the arccosine of a number in radians.
asin()Returns the arcsine of a number in radians.
atan()Returns the arctangent quotient of two numbers.
atan2()Returns a numeric value between -pi and pi in radians.
ceil()Returns the smallest integer greater than or equal to a number.
cos()Returns the cosine of a number in radians.
exp()Returns the exponential value of a number.
floor()Returns the largest integer less than or equal to a number.
log()Returns the natural logarithm of a number.
max()Returns the largest of some numbers.
min()Returns the smallest of some numbers.
pow()Returns base to the exponent power.
random()Returns a random number between 0 and 1.
round()Returns the value of the number rounded to the nearest integer.
sin()Returns the sine of a number in radians.
sqrt()Returns the positive square root of a number.
tan()Returns the tangent of a number in radians.

Instance Methods

We cannot instantiate Math objects and so there are no instance methods.

Examples



// Store properties in an array.
var mathProps = new Array(8);
mathProps[0] = Math.E; // Euler's constant
mathProps[1] = Math.LN2; // Natural log of 2
mathProps[2] = Math.LN10; // Natural log of 10
mathProps[3] = Math.LOG2E; // Base 2 log of E
mathProps[4] = Math.LOG10E; // Base 10 log of E
mathProps[5] = Math.PI; // Circumference to diameter ratio of a circle
mathProps[6] = Math.SQRT1_2; // Square root of 1/2
mathProps[7] = Math.SQRT2; // Square root of 2
alert(mathProps);  

Press the button below to action the above code:

Related Tutorials

JavaScript Advanced Tutorials - Lesson 4 - Math



  

go to home page Homepage go to top of page Top