JavaScript makes working with math easy. It provides us with the arithmetic operators you'd expect from a programming language, and offers us a useful built-in Math library that does a lot of work for us so that we can focus on writing great code instead.
Math is easy.
Arithmetic Operators
Here are some of the many arithmetic operators that allow you to manipulate numbers in JavaScript.
Addition
You can add two numbers together using the + operator. For example:
You can divide by an arbitrary by using division assignment. For example:
JAVASCRIPT
let peanuts = 100;
peanuts /= 20; // divides the value by 20console.log("I have " + peanuts + " peanuts left!");
HTML
I have 5 peanuts left!
Modulus
You can find the remainder of a division by using the % operator. For example:
JAVASCRIPT
const people = 20;
const peoplePerCar = 6;
const peopleInLastCar = people % peoplePerCar;
console.log("The last car will have " + peopleInLastCar + " people in it.");
HTML
The last car will have 2 people in it.
You can find the remainder of an arbitrary number by using modulus assignment. For example:
JAVASCRIPT
let slices = 45;
slices %= 7; // does modulus 7console.log("I have " + slices + " slices leftover!");
HTML
I have 3 slices leftover!
Order of Operations
Order of Operations, also known as PEMDAS, is at play here, including with the use of parentheses. For example:
JAVASCRIPT
const x = (5 * 3) + 10 / 2;
console.log("The value of x is " + x + ".");
The order of operations is very important because it determines how the numbers are calculated and how the values are displayed. For example, the above expression is evaluated as (5 * 3) + (10 / 2) which is 20 because the * operator has higher precedence than the + operator.
Math Properties
JavaScript's Math library, among other things, gives us access to mathematical properties that are commonly used so that we do not need to implement this ourselves. Let's look some examples below:
Euler's Number - Math.E
One of these constants is Euler's Number, via Math.E.
JAVASCRIPT
const euler = Math.E;
console.log(euler);
HTML
2.718281828459045
Pi - Math.PI
Pi is the ratio of the circumference of a circle relative to its diameter, and is accessed in JavaScript using Math.PI.
JAVASCRIPT
const pi = Math.PI;
console.log(pi);
HTML
3.141592653589793
Math Methods
Likewise with properties, JavaScript also implements some commonly used mathematical methods that we can use anytime we want.
Absolute Value
Getting the absolute value of a number is easy. Simple pass a number to Math.abs() and it will return what you expect. For example:
JAVASCRIPT
const a = 6;
const b = -13;
console.log("Absolute value of a is " + Math.abs(a) + ".");
console.log("Absolute value of b is " + Math.abs(b) + ".");
HTML
Absolute value of a is 6.
Absolute value of b is 13.
Floor
The floor method rounds a number down to the nearest integer. Put a number inside Math.floor() and you're good to go. For example:
JAVASCRIPT
const decimal = 24.942;
const floor = Math.floor(decimal);
console.log("The value of floor is " + floor + ".");
HTML
The value of floor is 24.
Ceiling
The ceiling method rounds a number up to the nearest integer. Put a number inside Math.ceil() and you're good to go. For example:
JAVASCRIPT
const decimal = 13.37;
const ceiling = Math.ceil(decimal);
console.log("The value of ceiling is " + ceiling + ".");
HTML
The value of ceiling is 14.
Logarithm
You can get the natural logarithm of a number using the Math.log() method. For example:
JAVASCRIPT
const number = Math.log(1);
console.log("The value of number is " + number + ".");
HTML
The value of number is 0.
Maximum
You can get the maximum of two numbers using the Math.max() method. For example:
JAVASCRIPT
const number = Math.max(14, 349);
console.log("The value of number is " + number + ".");
HTML
The value of number is 349.
Minimum
You can get the minimum of two numbers using the Math.min() method. For example:
JAVASCRIPT
const number = Math.min(53, 94);
console.log("The value of number is " + number + ".");
HTML
The value of number is 53.
Power
You can take the power of a number by using Math.pow(). For example:
JAVASCRIPT
const number = Math.pow(3, 2);
console.log("The value of number is " + number + ".");
HTML
The value of number is 9.
Random
To get a random number in JavaScript, there's a Math.random() method for that. By itself, it just returns a number between 0 and 1.
JAVASCRIPT
const random = Math.random();
console.log("The value of random is " + random + ".");
HTML
The value of random is 0.2156683652500082.
However, you can manipulate this fact to get a random number between two other numbers, like this:
JAVASCRIPT
functionrandomBetween(min, max) {
returnMath.floor(Math.random() * (max - min + 1)) + min;
}
const random = randomBetween(5, 25);
console.log("The value of random is " + random + ".");
HTML
The value of random is 15.
Round
You can round a number to the nearest integer using Math.round(). For example:
JAVASCRIPT
const rounded = Math.round(3.14159);
console.log("The value of rounded is " + rounded + ".");
HTML
The value of rounded is 3.
Square Root
You can take the square root of a number with Math.sqrt(). For example:
JAVASCRIPT
const root = Math.sqrt(49);
console.log("The value of root is " + root + ".");