Math object provides a range of mathematical constants and operations that are useful in a number of contexts. In this article will examine the Math object and provide some examples.
What is the Math Object ?
The Math object is a built-in object that provides a wide range of mathematical functions and constants. Rounding, trigonometry, logarithms, exponentials, and other mathematical operations can all be carried out by it.
This object heavily used in applications related to math and science. Developers can easily execute complicated mathematical operations without having to write their own functions from scratch.
The Math object's properties and methods are accessed using the dot notation.
Math.methodName(number);
Let's look at some of the math object's methods
Generating Random Numbers
The Math.random()
method generates a random number between 0 and 1 (1 is excluded). This method does not accept any arguments.
const randomNumber = Math.random();
console.log(randomNumber); //0.9407900228983954
If you try this code, you will have a different output since this method generates a random number.
Math.round
To round a number to the nearest integer, use the math.round() function. The number to be rounded is the function's only input.
The function rounds to the closest even integer if the value to be rounded is exactly halfway between two integers. The number will be rounded to the nearest whole number.
Let's look at an example.
let number = 4.4;
let rounded = Math.round(number);
console.log(rounded); // outputs 4
number = 4.6;
rounded = Math.round(number);
console.log(rounded); // outputs 5
number = 4.5;
rounded = Math.round(number);
console.log(rounded); // outputs 5
number = 5.5;
rounded = Math.round(number);
console.log(rounded); // outputs 6
Math.ceil
The Math.ceil()
function rounds up a number to the nearest integer. The number to be rounded is the only argument required by the function.
let number = 10.4;
let rounded = Math.ceil(number);
console.log(rounded); // outputs 11
number = -7.1;
rounded = Math.ceil(number);
console.log(rounded); // outputs -7
Math.floor
No matter how many decimal places the number has, the Math.floor()
function always rounds down to the nearest integer.
let number = 4.9;
let rounded = Math.floor(number);
console.log(rounded); // outputs 4
number = -6.9;
rounded = Math.floor(number);
console.log(rounded); // outputs -7
Math.trunc
The Math.trunc()
function returns the integer portion of the number unchanged after removing any decimal parts.
let number = 4.9;
let truncated = Math.trunc(number);
console.log(truncated); // outputs 4
number = -25.9;
truncated = Math.trunc(number);
console.log(truncated); // outputs -25
Math.pow
The Math.pow()
function raises a base number to an exponent's power.
```JavaScript syntax
Math.pow(base, exponent)
Let's look at an example.
```JavaScript
let base = 2;
let exponent = 3;
var result = Math.pow(base, exponent);
console.log(result); // outputs 8
Math.sqrt
The Math.sqrt()
function is used to determine a number's square root. The number to calculate the square root of is the only argument required by the function.
let number = 81;
let squareRoot = Math.sqrt(number);
console.log(squareRoot); // outputs 9
Math.abs
The Math.abs()
function is used to obtain a number's absolute value. The number that needs to have its absolute value determined is the only argument to the function.
let number = -5;
let absoluteValue = Math.abs(number);
console.log(absoluteValue); // outputs 5
number = 5;
absoluteValue = Math.abs(number);
console.log(absoluteValue); // outputs 5
It's important to understand that these functions do not modify the initial value; rather, they return a new rounded integer value. Additionally, these functions only accept numeric input and will return NaN
(Not a Number) if a non-numeric value is passed as argument.
These aren't the only functions available to use with the Math
objects but we've covered the most commonly used functions.
Top comments (0)