JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Math

The Math object in JavaScript provides a collection of properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All its properties and methods are static, meaning you access them using the Math prefix, not by creating a Math object instance. This object is extremely useful for performing mathematical tasks, ranging from basic arithmetic operations to complex calculations.

Syntax

Since Math is a static object, you use it directly without instantiation:

let result = Math.methodName(parameters);
  • methodName represents the Math method you wish to use.
  • parameters are the arguments that the method accepts.

Properties

The Math object includes properties for mathematical constants:

PropertyDescription
Math.EEuler's constant and the base of natural logarithms, approximately 2.718.
Math.LN2Natural logarithm of 2, approximately 0.693.
Math.LN10Natural logarithm of 10, approximately 2.303.
Math.LOG2EBase 2 logarithm of E, approximately 1.443.
Math.LOG10EBase 10 logarithm of E, approximately 0.434.
Math.PIRatio of the circumference of a circle to its diameter, approximately 3.14159.
Math.SQRT1_2Square root of 1/2, approximately 0.707.
Math.SQRT2Square root of 2, approximately 1.414.

Methods

Math object methods for performing various mathematical operations:

MethodDescription
Math.abs(x)Returns the absolute value of x.
Math.acos(x)Returns the arccosine of x, in radians.
Math.asin(x)Returns the arcsine of x, in radians.
Math.atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians.
Math.atan2(y, x)Returns the arctangent of the quotient of its arguments.
Math.ceil(x)Returns the smallest integer greater than or equal to x.
Math.cos(x)Returns the cosine of x (x is in radians).
Math.exp(x)Returns E<sup>x</sup>, where x is the argument, and E is Euler's constant (2.718...), the base of the natural logarithms.
Math.floor(x)Returns the largest integer less than or equal to x.
Math.log(x)Returns the natural logarithm (base E) of x.
Math.max(x, y, z, ...)Returns the number with the highest value.
Math.min(x, y, z, ...)Returns the number with the lowest value.
Math.pow(x, y)Returns the value of x to the power of y.
Math.random()Returns a random number between 0 (inclusive) and 1 (exclusive).
Math.round(x)Rounds x to the nearest integer.
Math.sin(x)Returns the sine of x (x is in radians).
Math.sqrt(x)Returns the square root of x.
Math.tan(x)Returns the tangent of an angle.

Examples

Example 1: Calculating the Area of a Circle

Javascript
Javascript

. . . .
  • This calculates the area of a circle using Math.PI and Math.pow() for the radius squared.

Example 2: Generating a Random Number

Javascript
Javascript

. . . .
  • Generates a random number between 0 (inclusive) and 1 (exclusive).

Example 3: Finding the Maximum and Minimum in a List of Numbers

Javascript
Javascript

. . . .
  • Finds the largest and smallest numbers in a list using Math.max() and Math.min().

Example 4: Rounding a Number

Javascript
Javascript

. . . .
  • Rounds a number to the nearest integer using Math.round().

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next