Javascript

Javascript Math Tutorial: How to do Math in Javascript

One of the most basic things we can do in Javascript is to perform simple mathematical operations. We can do this through simple operations, and also using the Javascript Math function.

Note: to do mathematical operations, we need to be comparing numbers. A number that is in quotation marks will not work in mathematical operations.

Mathematical Operations

Let's take a look at a simple mathematical operation:

javascript Copy
let i = 1; let j = i + 10;

Above, we have defined a variable i. We then define another variable, j, which is equal to i plus 10. Therefore, when we refer to j in our code, it will return 11.

There are a bunch of other mathematical operations we can do. Let's take a look.

All Arithmetic Operators

Operator What it does Code example
+ Adds two numbers together 10 + 5 returns 15
- Subtract one number from another 10 - 5 returns 5
+ Divide one number by another 10 / 5 returns 2
* Multiply two numbers together 10 * 5 returns 50
% Get the remainder of a calculation 12 % 5 returns 2 (12 divided by 5 leaves a remainder of 2)
** Raises one number to the power of another 2 ** 3 returns 8 (2 to the power of 3 is 8)

Math Constants

Apart from being able to do basic maths with operators in Javascript, we also have the Math object. This object lets us access mathematical constants and do some more complicated maths.

Let's start by looking at Mathematical constants Below is an example of how we can access PI.

javascript Copy
let pi = Math.PI; // returns 3.141...

In Javascript, we use a dot to note a specific child of the Math object. So the above will give us the value of PI. A list of all mathematical constants we can get is shown below. These are all irrational numbers.

Mathematical Constant What it does Value
Math.PI Returns PI 3.141 ...
Math.E Returns Euler's number 2.718 ...
Math.E Returns Euler's number 2.718 ...
Math.LN2 Returns ln(2) 0.693 ...
Math.LOG10E Returns log10(e) 0.434 ...
Math.LOG2E Returns log2(e) 1.442 ...
Math.LN10 Returns ln(10) 2.302 ...
Math.SQRT2 Returns the square root of 2 1.414 ...
Math.SQRT1_2 Returns the square root of 1/2 0.707 ...

Mathematical Notation in Javascript

We can use mathematical notation in Javascript. A mathematic notation for 3124 would look like this:

javascript Copy
let myMathNotation = 3.124e3 console.log(myMathNotation);

We use e to refer to the exponent. The above translates to 3.124 x 103.

Rounding and Static Methods

We can use the Math object to do some more complicated mathematics. These take a number as a value, and then adjust it.

Let's start by looking at how we perform rounding, as well as find the maximum or minimum of a set of numbers. These operations are listed below.

Abs

Math.abs takes a number and returns its absolute value. All that means, is it makes it positive if it was negative. It is the equivalent of the mathematic concept of magnitude.

javascript Copy
let absoluteNumber = Math.abs(-2); // Returns 2.

Floor

Math.floor takes a number and rounds it down. This will only work with decimals.

javascript Copy
let roundItDown = Math.floor(2.8371); // Returns 2.

Ceil

Math.ceil takes a number and rounds it up. This will only work with decimals.

javascript Copy
let roundItDown = Math.ceil(2.8371); // Returns 3.

Round

Math.round takes a number and rounds it. This will only work with decimals.

javascript Copy
let roundItDown = Math.ceil(1.453); // Returns 1.

Max

Math.max takes a set of numbers separated by commas, and picks the biggest one.

javascript Copy
let biggestNumber = Math.max(2, 3, 6); // Returns 6.

Min

Math.min takes a set of numbers separated by commas, and picks the smallest one.

javascript Copy
let smallestNumber = Math.min(2, 3, 6); // Returns 2.

More Static Methods

These useful operations let us manipulate or perform mathematical operations on numbers.

Pow

Math.pow takes a number, and raises it to a certain power. It does not punch anyone (pow pow).

javascript Copy
let toThePower = Math.pow(2, 3); // Returns 8, i.e. 2 to the power of 3.

Sign

Math.sign takes a number, and returns it sign. Not to be confused with sin

javascript Copy
let getSign = Math.sign(-14); // Returns -1 let getAnotherSign = Math.sign(14); // Returns 0

Trunc

Math.trunc takes a number, and returns only the integer part of it.

javascript Copy
let truncIt = Math.trunc(14.2431); // Returns 14

Sqrt

Math.sqrt takes a number, and returns its square root.

javascript Copy
let sqrtIt = Math.sqrt(4); // Returns 2

Cbrt

Math.sqrt takes a number, and returns its cube root.

javascript Copy
let cbrtIt = Math.sqrt(8); // Returns 2

Random

Math.random returns a random number between 0 and 1.

javascript Copy
let randomNumber = Math.random(); // As an example, may return 0.4845737088624795

Logs

There are a number of ways to do logs in Javascript, all of which are shown below:

javascript Copy
let naturalLog = Math.log(10); // The natural log of 10 let log10 = Math.log10(10); // The base 10 log of 10 let log2 = Math.log2(10); // The base 2 log of 10 let log2 = Math.log1p(10); // The natural log of 1 + 10

Operations for Geometry

There are a whole host of operations we can run to manipulate and calculate geometry. A list of these angle functions are shown below. For all of these, it is assumed that the number is in radians.

As well as the basic sin, cos and tan operations, we also have functions for the arc versions of each (denoted with an a in front of the function), and the hyperbolic versions (denoted with an h at the end).

javascript Copy
let x = 1.4; // Geometry mathematical functions Math.sin(x); Math.cos(x); Math.tan(x); Math.asin(x); Math.acos(x); Math.atan(x); Math.sinh(x); Math.cosh(x); Math.tanh(x); Math.asinh(x); Math.acosh(x); Math.atanh(x);

ParseFloat and ParseInt

Finally, let's look at a very useful function in Javascript known as parseFloat. As we have discussed in other articles, Javascript infers type. So "1" in quotation marks is not a number, but a string. The function parseFloat forces strings into a float, which can be a decimal or in standard mathematical notation parseInt, on the other hand, forces numbers into Integers, which have no decimals.

For example:

javascript Copy
let myNumber = "12345" // type: String let myOtherNumber = "1.3453"; // type: String // Let's parse our numbers let parseNumber = parseFloat(myNumber); // Returns 12345 type: Number let parseNumber = parseFloat(myOtherNumber); // Returns 1.3453 type: Number let parseNumber = parseInt(myOtherNumber); // Returns 1 type: Number

This means we can take numbers stuck in strings and convert them to real numeric types which we can do calculations with.

Last Updated Tuesday, 15 June 2021
Click to Subscribe Subscribed

Subscribe

Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email