Note: to do mathematical operations, we need to be comparing numbers. A number that is in quotation marks will not work in mathematical operations.
Let's take a look at a simple mathematical operation how we can use maths in variables which we covered here. Basic maths operations work just the same as if we were to write them on paper:
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 at them all.
|Operator||What it does||Code example|
|+||Adds two numbers together||
|-||Subtract one number from another||
|+||Divide one number by another||
|*||Multiply two numbers together||
|%||Get the remainder of a calculation||
|**||Raises one number to the power of another||
let x = 12; let y = 15; let add = x + y; let remainder = x % y; let raiseToPower = x ** y; let divide = x / y; console.log("Addition: " + add); console.log("Remainder: " + remainder); console.log("To the Power: " + raiseToPower); console.log("Divide: " + divide);
Let's start by looking at Mathematical constants Below is an example of how we can access PI.
let pi = Math.PI; // returns 3.141...
|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 ...|
let myMathNotation = 3.124e3 console.log(myMathNotation);
We use e to refer to the exponent. The above is the same as writing 3.124 x 103.
Rounding and Static Methods
We can use the Math object to do some more complicated mathematics, using functions that are built into it. These functions take a number as a value, and then adjust it accordingly. Let's look at a few examples.
Math.abs takes a number and returns its absolute value. All that means, is it makes it positive if it was negative, and keeps it positive if it already was. It is the equivalent of the mathematic concept of magnitude.
let absoluteNumber = Math.abs(-2); // Returns 2.
Math.floor takes a number and rounds it down. This will only work with decimals.
let roundItDown = Math.floor(2.8371); // Returns 2.
Math.ceil takes a number and rounds it up. This will only work with decimals.
let roundItDown = Math.ceil(2.8371); // Returns 3.
Math.round takes a number and rounds it. This will only work with decimals.
let roundItDown = Math.ceil(1.453); // Returns 1.
Math.max takes a set of numbers separated by commas, and picks the biggest one.
let biggestNumber = Math.max(2, 3, 6); // Returns 6.
Math.min takes a set of numbers separated by commas, and picks the smallest one.
let smallestNumber = Math.min(2, 3, 6); // Returns 2.
More Static Methods
These useful operations let us manipulate or perform mathematical operations on numbers.
Math.pow takes a number, and raises it to a certain power. It is similar to the
** arithmetic operator we looked at earlier.
let toThePower = Math.pow(2, 3); // Returns 8, i.e. 2 to the power of 3.
Math.sign takes a number, and returns it sign. Not to be confused with sin
let getSign = Math.sign(-14); // Returns -1 let getAnotherSign = Math.sign(14); // Returns 0
Math.trunc takes a number, and returns only the integer part of it.
let truncIt = Math.trunc(14.2431); // Returns 14
Math.sqrt takes a number, and returns its square root.
let sqrtIt = Math.sqrt(4); // Returns 2
Math.sqrt takes a number, and returns its cube root.
let cbrtIt = Math.sqrt(8); // Returns 2
Math.random returns a random number between 0 and 1.
let randomNumber = Math.random(); // As an example, may return 0.4845737088624795
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 geometry 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).
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
"5" to a real number, we can use the function
parseFloat (which changes the number to a float), or
parseInt, which changes the number to an integer - the difference of course being that an integer has no decimals, while a float does.
Both of these functions accept both numbers, and numbers in mathematical notation.
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
Math object. As well as that, we have a number of static functions we can use to manipulate numbers through the same
Math function. We've learned how to use all of these in this guide, as well as how to coerce a string of numbers into a number with
- How to Auto Generate Images with Node.JS and Canvas
- How to Stop Using jQuery
- Deleting an Item in an Array at a Specific Index