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:
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||
|-||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'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 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.
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.
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 does not punch anyone (pow pow).
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 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).
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
"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.
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.