The JavaScript Math object offers various properties and methods for performing mathematical operations, such as generating random numbers, rounding numbers, retrieving constants like PI, and executing complex calculations. It also includes methods for tasks that are typically challenging or impossible with basic arithmetic operators (+, -
, *
, and /
), such as computing sine or cosine values.
The Math.PI
property represents the ratio of a circle's circumference to its diameter. PI (π
) is a fundamental mathematical constant, approximately equal to 3.14159: Math.PI = π ≈ 3.14159
Here's an example that calculates the area of a circle using the Math.PI property.
// Printing PI value
document.write(Math.PI); // Prints: 3.141592653589793
// Function to calculate circle area
function calculateCircleArea(radius){
var area = (Math.PI) * radius * radius;
return area;
}
document.write(calculateCircleArea(5)); // Prints: 78.53981633974483
document.write(calculateCircleArea(10)); // Prints: 314.1592653589793
Note: The Math object is a native JavaScript object, so its properties and methods can be accessed directly. You do not need to create a Math object yourself; it is automatically available through the JavaScript interpreter.
The Math.abs()
method calculates the absolute (positive) value of a number. Thus, -1 becomes 1, -5 becomes 5, and so forth. Here's an example:
document.write(Math.abs(-1)); // Prints: 1
document.write(Math.abs(1)); // Prints: 1
document.write(Math.abs(-5)); // Prints: 5
document.write(Math.abs(-10.5)); // Prints: 10.5
The Math.random()
method generates a random floating-point number ranging from 0
(inclusive) to just below 1
(exclusive). If you need a random integer between zero and a higher integer, you can use the following approach:
document.write(Math.random()); // Expected output: a number between 0 and 1
// Function to create random integer
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
document.write(getRandomInt(3)); // Expected output: 0, 1 or 2
document.write(getRandomInt(1)); // Expected output: 0
The Math.sqrt()
method computes the square root of a number: Math.sqrt(x) = x
If the number is negative, it returns NaN
. Here's an example:
document.write(Math.sqrt(4)); // Prints: 2
document.write(Math.sqrt(16)); // Prints: 4
document.write(Math.sqrt(0.25)); // Prints: 0.5
document.write(Math.sqrt(-9)); // Prints: NaN
/* Function to calculate hypotenuse.
Hypotenuse is the longest side of a right-angled triangle. */
function calculateHypotenuse(a, b) {
return Math.sqrt((a * a) + (b * b));
}
document.write(calculateHypotenuse(3, 4)); // Prints: 5
document.write(calculateHypotenuse(5, 12)); // Prints: 13
The JavaScript Math object offers several methods for rounding numbers, each serving a distinct purpose. The following section will explain these methods in detail.
ceil()
MethodThe Math.ceil()
method rounds a number up to the next highest integer. For example, 3.5 becomes 4, and -5.7 becomes -5 (because -5 is greater than -6). Here's an example:
document.write(Math.ceil(3.5)); // Prints: 4
document.write(Math.ceil(-5.7)); // Prints: -5
document.write(Math.ceil(9.99)); // Prints: 10
document.write(Math.ceil(-9.99)); // Prints: -9
document.write(Math.ceil(0)); // Prints: 0
floor()
MethodThe Math.floor()
method rounds a number down to the next lowest integer. For example, 3.5 becomes 3, and -5.7 becomes -6 (because -6 is less than -5). Here's an example:
document.write(Math.floor(3.5)); // Prints: 3
document.write(Math.floor(-5.7)); // Prints: -6
document.write(Math.floor(9.99)); // Prints: 9
document.write(Math.floor(-9.99)); // Prints: -10
document.write(Math.floor(0)); // Prints: 0
round()
MethodThe Math.round()
method rounds a number to the nearest integer. If the decimal part is .5
or greater, the number is rounded up; otherwise, it is rounded down. For example, 3.5 becomes 4, -5.7 becomes -6, 4.49 becomes 4, and so on. Here's an example:
document.write(Math.round(3.5)); // Prints: 4
document.write(Math.round(-5.7)); // Prints: -6
document.write(Math.round(7.25)); // Prints: 7
document.write(Math.round(4.49)); // Prints: 4
document.write(Math.round(0)); // Prints: 0
The Math.max()
and Math.min()
methods are used to determine the largest or smallest number in a set of numbers, respectively. Here's an example:
document.write(Math.max(1, 3, 2)); // Prints: 3
document.write(Math.max(-1, -3, -2)); // Prints: -1
document.write(Math.min(1, 3, 2)); // Prints: 1
document.write(Math.min(-1, -3, -2)); // Prints: -3
You can also find the maximum or minimum value within an array or an array-like object using the apply()
method, demonstrated in the following example:
var numbers = [1, 3, 2];
document.write(Math.max.apply(null, numbers)); // Prints: 3
document.write(Math.min.apply(null, numbers)); // Prints: 1
There's an even simpler way to achieve this. In ECMAScript 6, you can accomplish the same task using the new spread operator (...
), as illustrated in the example below:
var numbers = [1, 3, 2];
document.write(Math.max(...numbers)); // Prints: 3
document.write(Math.min(...numbers)); // Prints: 1
The Math.pow()
method is used to raise a number to a specified power.
The expression Math.pow(x, y)
, which is equivalent to xy
mathematically, indicates how many times the base x
is multiplied by itself y times. Here's an example:
document.write(Math.pow(3, 2)); // Prints: 9
document.write(Math.pow(0, 1)); // Prints: 0
document.write(Math.pow(5, -2)); // Prints: 0.04
document.write(Math.pow(16, 0.5)); // Prints: 4 (square root of 4)
document.write(Math.pow(8, 1/3)); // Prints: 2 (cube root of 8)
A positive exponent indicates multiplication (52 = 5 x 5 = 25), a negative exponent indicates division (5-2 = 1/52 = 0.04), while a fractional exponent indicates a root of the base.
The JavaScript Math object also includes several trigonometric methods such as sin()
, cos()
, and tan()
to perform trigonometric operations. These methods work in radians, so you need to convert any degree measurements by multiplying them by π/180
before using them.
Since π
radians are equivalent to 180 degrees: π rad = 180°
. Therefore, π/2
radians equals 90 degrees, π/3
radians equals 60 degrees, and so forth. Here's an example:
document.write(Math.sin(0 * Math.PI / 180)); // Prints: 0
document.write(Math.sin(90 * Math.PI / 180)); // Prints: 1
document.write(Math.cos(0 * Math.PI / 180)); // Prints: 1
document.write(Math.cos(180 * Math.PI / 180)); // Prints: -1
document.write(Math.tan(0 * Math.PI / 180)); // Prints: 0