Introduction
Apart from the very basic Number
object, we have a real giant for playing with numbers in JavaScript that is the Math
object.
It comes with a whole ton of properties and methods for dealing with numbers in different aspects - it has got functions for calculating angles, logs, exponents, rounding numbers and a lot more.
A number-intensive JavaScript application like a POS program can't do much without using the Math object, at least in one place. The places where it gets employed are obviously more than just one!
In this chapter we will specifically look at the useful bucket of properties and methods on the Math
object. To work best with this giant object it is recommended that you know concepts in maths like trigonometry, logarithms, indices etc
Properties
First come first serve, let's begin with the properties of Math
:
Math.PI
Math.PI
holds the value of the mathematical constant π.
Math.PI; // returns 3.141592653589793
PI
is case-sensitive - writing pI
orpi
would return undefined
.Math.E
Math.E
returns the Euler's number - ⅇ.
Math.E; // returns 2.718281828459045
Methods
Now onto the methods of the Math
object.
Math.round()
Math.round()
rounds a number to the nearest integer.
Math.round(1.2); // returns 1
Math.round(1.5); // returns 2
Math.round(1.9); // returns 2
Math.round(Math.PI); // returns 3
Math.ceil()
Math.ceil()
rounds a number to the smallest integer greater than it. You can think of it like this: a ceiling is the top of a room - therefore a number ceiled goes to its top integer.
Math.ceil(1.2); // returns 2
Math.ceil(1.5); // returns 2
Math.ceil(1.9); // returns 2
Math.ceil(Math.PI); // returns 4
Math.floor()
Math.floor()
rounds a number to the largest integer smaller than it. You can think of it like this: a floor is the bottom of a room - therefore a number floored goes to its bottom integer.
Math.floor(1.2); // returns 1
Math.floor(1.5); // returns 1
Math.floor(1.9); // returns 1
Math.floor(Math.PI); // returns 3
Math.pow()
Math.pow()
raises its first argument to the power of the second argument.
Math.pow(2, 2); // returns 4
Math.pow(2, 3); // returns 8
Math.pow(2, 4); // returns 16
Math.pow(4, 0.5); // returns 2
Math.max()
Math.max()
returns the maximum number from a given list of arguments.
Math.max(1, 2, 3, 4); // returns 4
Math.max(-1, -2, -5); // returns -1
Math.max(1.5, 1.45, 1.48, 1.497); // returns 1.5
Math.max(-1.2, -1.1); // returns -1.1
Math.min()
Math.min()
returns the minimum number from a given list of arguments.
Math.min(1, 2, 3, 4); // returns 1
Math.min(-1, -2, -5); // returns -5
Math.min(1.5, 1.45, 1.48, 1.497); // returns 1.45
Math.min(-1.2, -1.1); // returns -1.2
Math.abs()
Math.abs()
gives the absolute value of a number.
Math.abs(2); // returns 2
Math.abs(-2); // returns 2
Math.abs(-2.5); // returns 2.5
Math.random()
Math.random()
returns a pseudo-generated random number between 0 (inclusive) and 1 (exclusive).
Math.random(); // something between 0-1
Try clicking the following box to see some randomness in action!
Math.sin()
, Math.cos()
and Math.tan()
Math.sin(*)
, Math.cos()
and Math.tan()
return the sin, cosine and tangents of angles (in radians) respectively.
// same as sin(0.5π) in maths
Math.sin(0.5 * Math.PI); // returns 1
Math.cos(0.5 * Math.PI); // returns 6.123233995736766e-17 that's almost 0
// Trick to return calculator value
Math.round(Math.cos(0.5 * Math.PI)); // returns 0
Math.tan(0.25 * Math.PI); // returns 0.9999999999999999
// Same trick
Math.round(Math.tan(0.25 * Math.PI)); // returns 1
And there is even more to this.... the list doesn't stop! Go to the console and start exploring!