JavaScript Math Object With Example

JavaScript Math Object

The math object provides the properties and methods for mathematical constants and functions.

Unlike the other global objects, Math is not a constructor.

All properties and methods of Math are static and can be called by using Math as an objcet without creating it.

Example:


  var pi_val = Math.PI;//Math object need not be created
  var sine_val = Math.sin(30);

Math Properties

Property Description
E Returns Euler's number, the base of natural logarithms, e, approximately 2.718
LN2 Returns the natural logarithm of 2, approximately 0.693
LN10 Returns the natural logarithm of 10, approximately 2.302
LOG2E Returns the base 2 logarithm of e, approximately 1.442
LOG10E Returns the base 10 logarithm of e, approximately 0.434
PI Returns the ratio of the circumference of a circle to its diameter (i.e. π). The approximate value of PI is 3.14159
SQRT1_2 Returns the square root of 1/2, approximately 0.707
SQRT2 Returns the square root of 2, approximately 1.414


JavaScript Math Methods

Method Description
abs() Returns the absolute value of a number.
acos() Returns the arccosine of a number, in radians.
acosh() Returns the hyperbolic arccosine of a number.
asin() Returns the arcsine of a number, in radians
asinh() Returns the hyperbolic arcsine of a number.
atan() Returns the arctangent of a number, in radians.
atan2(y, x) Returns the arctangent of the quotient of its arguments.
atanh() Returns the hyperbolic arctangent of a number.
cbrt() Returns the cube root of a number.
ceil() Returns the next integer greater than or equal to a given number (rounding up).
cos() Returns the cosine of the specified angle. The angle must be specified in radians.
cosh() Returns the hyperbolic cosine of a number.
exp(x) Returns ex, where x is the argument, and e is Euler's number (also known as Napier's constant), the base of the natural logarithms.
floor() Returns the next integer less than or equal to a given number (rounding down).
log() Returns the natural logarithm (base e) of a number.
max(x, y, ...) Returns the highest-valued number in a list of numbers.
min(x, y, ...) Returns the lowest-valued number in a list of numbers.
pow(x, y) Returns the base to the exponent power, that is, xy.
random() Returns a random number between 0 and 1 (including 0, but not 1).
round() Returns the value of a number rounded to the nearest integer.
sin() Returns the sign of a number (given in radians).
sinh() Returns the hyperbolic sine of a number.
sqrt() Returns the square root of a number.
tan() Returns the tangent of a number.
tanh() Returns the hyperbolic tangent of a number.
trunc(x) Returns the integer part of a number by removing any fractional digits.


Read Also:


Card image cap