Brief Background
JavaScript has several classes, and methods that come pre-packed. Classes are essentially objects and can be accessed and utilized as objects. Another example of a JavaScript object is the Date object. You can read more about it in this article that I wrote about the Date object. This article, in particular, will explore and demonstrate the several methods that are available within the Math object.
Before diving into the deep end ๐, I'd like to explore exactly how methods work in the context of an object. Consider the JavaScript code block below.
let hobbies = {
morning: 'coffee & eggs',
midday: 'burger',
lateAfternoon: 'more coffee',
night : function(){
console.log("Shhhh, I'm sleeping!")
}
}
console.log(hobbies.night)
The JavaScript code above is an object definition that outputs 'Shhhh, I'm sleeping!'. The night property of the hobbies object can be referenced just like any other property like hobbies.midday. This also allows us to reference this method using bracket notation e.g. hobbies['night'].
Advanced JavaScript users have probably come across some of the methods listed below. If it is your first time coming across this, then let's have a look at some methods in the Math object.
Math.abs() - Absolute Value
The Math.abs() method returns the absolute value of a number (the value without regard to whether it is positive or negative). For example, the absolute value of -5 is the same as the absolute value of 5. Math.abs takes a numeric expression as a parameter for example Math.abs(-5) will return 5.
Math.atan() - Arctangent
This is a method that returns the arctangent of a number. The Math.atan() argument accepts one argument which has to be a numeric expression for which the arctangent is needed. For example, calling Math.atan(5) will output 1.373400766945016, and calling it with no argument such as Math.atan() will return NaN.
Math.ceil() - Ceiling
Math.ceil() accepts one argument which must be a numeric expression, it returns the smallest integer greater than or equal to its numeric argument. Consider the examples below.
Math.ceil(2.2)will return 3.Math.ceil(2)will return 2.Math.ceil(2 * 3.2)will return 7.
Math.cos() - Cosine
The Math.cos() method returns the cosine of a number. When called, it accepts an argument that should be a numeric expression that contains an angle measured in radians. For example Math.cos(8*8) will return 0.39185723042955.
Math.exp() - Natural Logarithm Base
This method returns e (the base of natural logarithms) raised to a power. It requires an argument of a numeric expression representing the power of e. For example Math.exp(10) will output 22026.465794806718, while Math.exp(2*2) will output 54.598150033144236.
Math.floor() - Floor
You can call floor() on the Math object which returns the greatest integer less than or equal to its numeric argument. It requires one argument which must be a numeric expression. If we re-use the examples we used earlier for Math.ceil(), we get different results as shown below.
Math.floor(2.2)will return 2.Math.floor(2)will return 2.Math.floor(2 * 3.2)will return 6.
The Math.floor() method behaves exactly the opposite way that Math.ceil() does.
Math.hypot() - Hypotenuse
This method returns the square root of the sum of squares of its arguments. Arguments passed into this function should be values to compute the square root for. If no arguments are passed, the result is +0. If there is only one argument, the result is the absolute value. If any argument is +Infinity or -Infinity, the result is +Infinity. If any argument is NaN, the result is NaN. If all arguments are either +0 or โ0, the result is +0. Math.hypot(-6*50) will return 300. Whereas Math.hypot(5, 10, 6, 22) will return 25.39685019840059.
Math.log10() - Base 10 Logarithm
To successfully call `Math.log10(), one argument must be passed and must be a numeric expression. The result will be the base 10 logarithm of a number. For example:
Math.log10(10)will return 1.Math.log10(50)will return 1.6989700043360187.
Math.max() - Maximum
The Math.max() method returns the larger of a set of supplied numeric expressions, it accepts a numeric expression to be evaluated. For example:
Math.max(5, 10)will return 10.Math.max(20+2, 4+4)will return 22.- Calling this method with no arguments will return -Infinity e.g.
Math.max().
๐Another cool thing you can do is to define an array such as let myList = [5, 6, 7, 20, -1, 6];, and then pass it as an argument using the JavaScript spread operator e.g. Math.max(...myList), this will return 20.
Math.min() - Minimum
This is the literal opposite of the Math.max() method defined above. The Math.min() method returns the smallest of a set of supplied numeric expressions and accepts a numeric expression as an argument to be evaluated. We can test it with the examples from the previous Max method.
Math.min(5, 10)will return 5.Math.min(20+2, 4+4)will return 8.- Calling this method with no arguments will return Infinity e.g.
Math.min(), however we can writeMath.min(Infinity, -Infinity)which will output -Infinity.
Math.PI - Pi๐ฅง
By far my favorite method from the Math object. This method returns the value of Pi which is the ratio of the circumference of a circle to its diameter. Unlike the previous methods, it doesn't accept any arguments and is referenced as a JavaScript object property. For example Math.PI will return 3.141592653589793.
Math.pow() - Power
Math.pow() returns the value of a base expression taken to a specified power. This method has to be called by passing two arguments, passing one argument will return NaN. The first argument is the base and the second is the power as shown below.
Math.pow(2.5)will return Nan.Math.pow(3, 5)will return 243.Math.pow(Infinity, 2)will return Infinity.Math.pow(0, 1)will return 0.Math.pow(2, 0)will return 1.
Math.round() - Rounded To
The round method returns a supplied numeric expression rounded to the nearest integer. It accepts one argument which must be the value to be rounded to the nearest integer. Consider the examples below.
Math.round(4.1)will return 4.Math.round(4.4)will return 4.Math.round(4.6)will return 5.Math.round(4.5*2)will return 9.
This method has the capabilities of both the Math.floor() and Math.ceil() methods.
Math.random() - Random
This is a method that I use a lot when I code in my day-to-day projects. Math.random() returns a pseudorandom number between 0 and 1. For example Math.random() will generate and return something like 0.06030215849000098.
If you do something like Math.floor(Math.random() * 2), this will randomly output either 1 or 0. We can even modify this to spit out a random number between 0 and 10 e.g. Math.floor(Math.random() * 10). For a larger range of values, change the 10 to something else like 100 or 100,000.
Math.sqrt() - Square root
This method returns the square root of a number. For example, if 8 times 8 equals 64, then the square root of 64 is 8. The Math.sqrt() accepts one argument which should be a numeric expression.
Math.sqrt(8+8)will return 4.Math.sqrt(128)will return 11.313708498984761.Math.sqrt(32/2)will return 4.Math.sqrt(256)will return 16.
Notable Mentions ๐๐พ
The list of JavaScript Math methods described so far are the main ones that people use and you will probably come across. Below is a list of other methods of the Math object that are, in my opinion, less important because I hardly or have never used them.
Math.acos()- Returns the arc cosine (or inverse cosine) of a number.Math.acosh()- Returns the inverse hyperbolic cosine of a number.Math.asin()- Returns the arcsine of a number.Math.asinh()- Returns the inverse hyperbolic sine of a number.Math.asinh()Math.atan2()- Returns the angle (in radians) from the X-axis to a point.Math.cbrt()- Returns an implementation-dependent approximation to the cube root of a number.Math.clz32()- Returns the number of leading zero bits in the 32-bit binary representation of a number.Math.cosh()- Returns the hyperbolic cosine of a number.Math.E- Returns the mathematical constant e. This is Euler's number, the base of natural logarithms (2.718281828459045).Math.expm1()- Returns the result of (e^x - 1), which is an implementation-dependent approximation to subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).Math.fround()- Returns the nearest single precision float representation of a number.Math.tan()- Returns the tangent of a number.Math.trunc()- Returns the integral part of a numeric expression, x, removing any fractional digits. If x is already an integer, the result is x.
If you enjoyed this article, you are welcome to share your opinion with me. Thank you for reading.
