What You Need To Know About JavaScript Math Operations
Posted by TotalDCSimply, the JavaScript Math object provides several useful properties and methods for performing mathematical tasks like generating random numbers, rounding numbers, obtaining values such as PI, and performing calculations, etc. It also helps when performing mathematical tasks that are too complex to perform using standard mathematical operators.
The Math.PI Property In JavaScript
The Math.PI property represents the ratio of the circumference of a circle to its diameter. PI (π) is a mathematical constant, which is approximately 3.14159: Math.PI = π ≈ 3.14159.
Here is an example that calculates the area of a circle using the Math.PI property:
// Printing PI value
console.log(Math.PI); // Prints: 3.141592653589793
// Function to calculate circle area
function calculateCircleArea(radius){
let area = (Math.PI) * radius * radius;
return area;
}
console.log(calculateCircleArea(5)); // Result: 78.53981633974483
console.log(calculateCircleArea(10)); // Result: 314.1592653589793
The Math object is a built-in JavaScript object so its properties and methods can be accessed directly. You don’t need to create a Math object, because it is automatically created by the JavaScript interpreter.
How To Get The Absolute Value In JavaScript
For this task, you need Math.abs() method. Math.abs() method is used to calculate the absolute value of a number. Here’s how it looks:
console.log(Math.abs(-1)); // Result: 1
console.log(Math.abs(1)); // Result: 1
console.log(Math.abs(-5)); // Result: 5
console.log(Math.abs(-10.5)); // Result: 10.5
How To Generate A Random Number In JavaScript
That can be done with Math.random() method. Math.random() method is used to generate a floating-point random number in the range from 0 up to but not including 1. What if you need to generate a bigger number than 1? You have to pass an integer as a parameter of the function up to which random number you want to get. Here’s an example of that:
console.log(Math.random()); // Result: a number between 0 and 1
// Function to create random integer
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
console.log(getRandomInt(3)); // Result: 0, 1 or 2
console.log(getRandomInt(1)); // Result: 0
How To Calculate The Square Root Of A Number In JavaScript
The Math.sqrt() method is used to calculate the square root of a number. If the number is negative, NaN is returned. Here is an example:
console.log(Math.sqrt(4)); // Result: 2
console.log(Math.sqrt(16)); // Result: 4
console.log(Math.sqrt(0.25)); // Result: 0.5
console.log(Math.sqrt(-9)); // Result: NaN
How To Round Numbers In JavaScript
The JavaScript Math object has few methods to round numbers, each one has its way of rounding the numbers. Let’s look at the examples.
The ceil() Method In JavaScript
The Math.ceil() method rounds a number up to the next highest integer. So, 3.5 becomes 4, -5.7 becomes -5 (because -5 is greater than -6), etc. Here’s an example:
console.log(Math.ceil(3.5)); // Result: 4
console.log(Math.ceil(-5.7)); // Result: -5
console.log(Math.ceil(9.99)); // Result: 10
console.log(Math.ceil(-9.99)); // Result: -9
console.log(Math.ceil(0)); // Result: 0
The floor() Method In JavaScript
The Math.floor() method rounds a number down to the next lowest integer. So, 3.5 becomes 3, -5.7 becomes -6 (because -6 is lesser than -5), etc. Here’s an example:
console.log(Math.floor(3.5)); // Result: 3
console.log(Math.floor(-5.7)); // Result: -6
console.log(Math.floor(9.99)); // Result: 9
console.log(Math.floor(-9.99)); // Result: -10
console.log(Math.floor(0)); // Result: 0
The round() Method In JavaScript
The Math.round() method rounds a number to the nearest integer in such a way that if the decimal part is .5 or greater, the number is rounded up, otherwise rounded down. Let’s look at the example:
console.log(Math.round(3.5)); // Result: 4
console.log(Math.round(-5.7)); // Result: -6
console.log(Math.round(7.25)); // Result: 7
console.log(Math.round(4.49)); // Result: 4
console.log(Math.round(0)); // Result: 0
How To Find The Largest And The Smallest Numbers In JavaScript
As you may already guess the Math.max() and Math.min() methods are used to find which number is the largest and which is the smallest in a group of numbers. Here’s an example:
console.log(Math.max(1, 3, 2)); // Result: 3
console.log(Math.max(-1, -3, -2)); // Result: -1
console.log(Math.min(1, 3, 2)); // Result: 1
console.log(Math.min(-1, -3, -2)); // Result: -3
Another way to to find the max or min value in for example array is to use apply() method. Here’s an example of that:
let numbers = [1, 3, 2];
console.log(Math.max.apply(null, numbers)); // Result: 3
console.log(Math.min.apply(null, numbers)); // Result: 1
Or you can even use the spread operator (…). Here’s how:
let numbers = [1, 3, 2];
console.log(Math.max(...numbers)); // Result: 3
console.log(Math.min(...numbers)); // Result: 1
How To Raise Number To A Power In JavaScript
There’s the Math.pow() method. Math.pow() method is used to raise the number to a specified power.
The syntax of Math.pow() method looks like this – Math.pow(x,y), where y shows how many times x is multiplied. Here’s an example:
console.log(Math.pow(3, 2)); // Result: 9
console.log(Math.pow(0, 1)); // Result: 0
console.log(Math.pow(5, -2)); // Result: 0.04
console.log(Math.pow(16, 0.5)); // Result: 4 (square root of 4)
console.log(Math.pow(8, 1/3)); // Result: 2 (cube root of 8)
How To Perform Trigonometric Operations In JavaScript
The JavaScript’s Math object also has several trigonometric methods like sin(), cos(), tan() to perform trigonometric operations. Here’s an example for you:
console.log(Math.sin(0 * Math.PI / 180)); // Result: 0
console.log(Math.sin(90 * Math.PI / 180)); // Result: 1
console.log(Math.cos(0 * Math.PI / 180)); // Result: 1
console.log(Math.cos(180 * Math.PI / 180)); // Result: -1
console.log(Math.tan(0 * Math.PI / 180)); // Result: 0
You can find previous articles about date and time in JavaScript here.