10 Most Common JavaScript Math Operations
The Math object in JavaScript provides multiple useful methods to perform mathematical operations. Going through some of the Math
objects you might think, how it differs from the Number methods. Well, the Math object allows us to work with more advanced calculations, such as finding absolute value, rounded numbers, random numbers, pi, maximum, and minimum integers, etc.
Use of Math Objects
Math.abs()
The Math.abs(x)
method returns the absolute value of the given element x
or an operation.
let x = -12Math.abs(x) //Output: 12let x = 2 - 4Math.abs(x) //Output: 2
2. Math.random()
The Math.random()
method is used to generate a random number between 0 to 1.
Math.random() //Output: 0.2128405351277376
3. Math.round()
The Math.round()
method returns the rounded value of the given decimal number.
Math.round(7.7) //Output: 8Math.round(7.4) //Output: 7
4. Math.ceil()
The Math.ceil(x)
method is used to rounds up the given number to the next highest integer.
//example 1
Math.ceil(9.8) //Output: 10//example 2
Math.ceil(9.4) //Output: 10
In the 2nd example even though the given decimal value 9.4
is less than .5
, using Math.ceil()
we can force it to round upward and return 10.
5. Math.floor()
Oppose to the Math.ceil()
method Math.floor()
is used to round down the given number to the next lowest integer.
Math.floor(9.5) //Output: 9
The given number 9.5
, has decimal digit greater than 0.5
yet the method returns 9, rounded downward.
6. Math.max()
The Math.max()
method is used to find the highest number from a group of numbers such as an array.
var arr = [2, 4, 77, 120, 0]Math.max(arr) //Output: 120
7. Math.min()
The Math.min()
method is used to find the lowest number from a group of numbers such as an array.
var arr = [2, 4, 77, 120, 0]Math.max(arr) //Output: 0
8. Math.pow()
The Math.pow(x, y)
method is the same as the mathematical expression x^y, returns the exponential value of the base number. It takes in two parameters, x
is the base and y
is the exponent, number of time the base value x
will be multiplied by x
.
let power = Math.pow(2, 4) //Output: 16
console.log(power) // 2^4 = 16
9. Math.sqrt()
The Math.sqrt()
method is the same as the mathematical expression √x; returns the square root of the number passed in as a parameter.
Math.sqrt(16) // Output: 4
10. Math.PI
The Math.PI
mathod returns the value of pi. Here, PI
is a property inside the Math object, which already is a fixed number.
Math.PI //Output: 3.141592653589793
Previous posts:
12 Most Common JavaScript Number Methods
12 Must KnowArray Methods for the Next Interview — JavaScript
Arrays: Left Rotation (JavaScript)
How to deploy Rails App on Heroku?
How to Make Your LinkedIn Profile Stand Out & Get Better Opportunities in 2020?
How to Create Simple Forms in React?
How To Solve Loop Detection Interview Question? | Crack The Coding Interview (6th edition)
Markdown — A Easier & Fast to Learn Markup Language
Data Structure — Linked List| SINGLY LINKED LIST PRACTICE(LeetCode)
Debug JS Code with DevTool Network Tab