Javascript
JavaScript provides the Math
object to facilitate a wide range of mathematical operations. The Math
object simplifies complex calculations, offering built-in functions and constants that developers can leverage.
Addition, Subtraction, Multiplication, and Division:
Here is an example of Math
object that supports fundamental arithmetic operations, allowing developers to perform calculations easily.
let sum = Math.add(3, 4); //Output: 7
let difference = Math.subtract(7, 2); //Output: 5
let product = Math.multiply(5, 6); //Output: 30
let quotient = Math.divide(10, 2); //Output: 5
Modulo Operator (%):
Retrieve the remainder of a division operation.
let remainder = Math.modulo(10, 3); //output: 1
JavaScript also offers mathematical properties or constants that can be useful in Code.
The mathematical constant Pi can be accessed using Math.PI
:
let circleArea = Math.PI * radius * radius;
Similarly, Euler’s number is accessible via Math.E
:
let exponentialResult = Math.pow(Math.E, 2);
JavaScript provides a range of built-in methods under the Math
object for more advanced mathematical operations.
Method | Description |
---|---|
Math.abs(x) | Returns the absolute value of a number x. |
Math.ceil(x) | Rounds a number x up to the nearest integer, and returns the result. |
Math.floor(x) | Rounds a number x down to the nearest integer, and returns the result. |
Math.round(x) | Rounds a number x to the nearest integer, rounding halfway cases towards the nearest even number. |
Math.max(x, y, …) | Returns the highest value among the provided numbers. |
Math.min(x, y, …) | Returns the lowest value among the provided numbers. |
Math.pow(x, y) | Returns the result of raising x to the power of y. |
Math.sqrt(x) | Returns the square root of a non-negative number x. |
Math.random() | Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive). |
Math.sin(x) | Returns the sine of an angle in radians. |
Math.cos(x) | Returns the cosine of an angle in radians. |
Math.tan(x) | Returns the tangent of an angle in radians. |
Math.PI | A mathematical constant representing the ratio of a circle’s circumference to its diameter (π). |
Math.E | Euler’s number, the base of the natural logarithm. |
Let’s see some useful methods and their examples.
Math.abs(x)
- Absolute Value:
x
.let absoluteValue = Math.abs(-5); // Returns 5
Math.ceil(x)
- Ceiling:
Rounds a number x
up to the nearest integer.
Example:
let roundedUp = Math.ceil(4.3); // Returns 5
Math.floor(x)
- Floor:
Rounds a number x
down to the nearest integer.
Example:
let roundedDown = Math.floor(4.9); // Returns 4
Math.round(x)
- Round:
Rounds a number x
to the nearest integer, rounding halfway cases towards the nearest even number.
Example:
let rounded = Math.round(4.5); // Returns 4
Math.max(x, y, ...)
- Maximum:
Returns the highest value among the provided numbers.
Example:
let maxValue = Math.max(2, 5, 1, 8); // Returns 8
Math.min(x, y, ...)
- Minimum:
Returns the lowest value among the provided numbers.
Example:
let minValue = Math.min(2, 5, 1, 8); // Returns 1
Math.pow(x, y)
- Power:
Returns the result of raising x
to the power of y
.
Example:
let powerResult = Math.pow(2, 3); // Returns 8
Math.sqrt(x)
- Square Root:
Returns the square root of a non-negative number x
.
Example:
let squareRoot = Math.sqrt(16); // Returns 4
Math.random()
- Random Number:
Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
Example:
let randomNum = Math.random(); // Returns a value between 0 and 1
Math.sin(x)
- Sine:
Returns the sine of an angle in radians.
Example:
let sineValue = Math.sin(Math.PI / 2); // Returns 1 (sine of 90 degrees)
These examples showcase the versatility of the Math
object in performing various mathematical operations in JavaScript
Sometimes, we may need to convert a floating-point number to an integer. JavaScript’s Math
object can assist with that:
let floatNumber = 5.78;
let integerNumber = Math.floor(floatNumber);
console.log(integerNumber); // Outputs 5