Computers & ProgrammingFrontend DevelopmentJavaScript

JavaScript Math Object

The JavaScript Math object is built-in to JavaScript and includes mathematical constants and functions. The Math object is not a constructor. Unlike other objects, it is not necessary to create the Math object before using it.

String Object Syntax

var a = Math.property;
document.write(Math.method); 

Math Object Properties

JavaScript provides eight mathematical properties (constants) that can be accessed from the Math object.

Property Description
E The mathematical constant e. This is Euler’s number; the base of natural logarithms.
LN2 The natural logarithm of 2.
LN10 The natural logarithm of 10.
LOG2E Base 2 logarithm of E.
LOG10E Base 10 logarithm of E.
PI Returns the ratio of the circumference of a circle to its diameter; PI.
SQRT1_2 The square root of 1/2.
SQRT2 The square root of 2.

Here is an example of how to use the PI property:

document.write(Math.PI);

Math Object Methods

The following table lists the methods of the Math object.

Method Description
abs(x) Returns the absolute value of x.
acos(x) Returns the arccosine of x.
asin(x) Returns the arcsine of x.
atan(x) Returns the arctangent of a number.
atan2(y,x) Returns the angle from the X-axis to a point represented by the supplied y and x coordinates.
ceil(x) Returns x, rounded upwards to the nearest integer.
cos(x) Returns the cosine of x.
exp(x) Returns e raised to a power.
floor(x) Returns x, rounded downwards to the nearest integer.
log(x) Returns the natural logarithm of x.
max(x,y,z,...,n) Returns the number with the highest value.
min(x,y,z,...,n) Returns the number with the lowest value.
pow(x,y) Returns the value of x to the power of y.
random() Returns a random number between 0 and 1.
round(x) Rounds x to the nearest integer.
sin(x) Returns the sine of x.
sqrt(x) Returns the square root of x.
tan(x) Returns the tangent of an angle.

Here is an example of how to use the Math max method:

document.write(Math.max(5,7,9,10,2,1));

Always keep in mind that JavaScript is case sensitive.

Leave a Comment

Your email address will not be published.

Scroll to Top