Numbers
In many programming languages working with numbers can be quite complicated. There are actually many different data types that needs to be remembered and set for each of the programs numbers. For instance integers must be declared with int, floating numbers with float. And then there's smallint, tinyint, double etc. A variable holding an integer can't later on be assigned a float value, then the whole program will crash since they have a different data types, so it needs to be converted first. This is always a struggle.
But not in javascript fortunately enough! Here we're spared from all this and it's nothing you even have to think about. At least when it comes to numbers, since javascript only uses one data type for all types of numbers which is the 64-bits binary floating point. It can contain numbers from 5e-324 to 1.7976931348623157e+308. Negative and positive, integers and floats. However they start losing their precision after about 9007199254740992 because it gets hard to round off exact at that size. But then we're up in extremely big numbers and there's always ways to get around that.
var num1 = 25;
var num2 = 12.5;
var num3 = 32.5;
var sum = num1 + num2 + num3; // 70
Operators
In javascript it's possible to do very complicated math if you want. Anything is possible and operators for addition, subtraction, multiplication and division are available. How they are used is pretty straightforward and is shown in the example below.
var x = 10;
x = x + 5; // Addition
x = x - 2; // Subtraction
x = x * 3; // Multiplication
x = x / 4; // Division
x = x % 3; // Modulo
// Simplified
x += 5;
x -= 2;
x *= 3;
x /= 4;
x %= 3;
// Even simpler for adding or subtracting 1
x++;
x--;
To decide the order on how the operators should be executed you do as usual in math and use parentheses. For example in the statement var x = 5 + 4 * (1 + 2) first 1 + 2 will be calculated since they're inside a parentheses, then the rest and lastly the variable x is assigned the result. No more complicated than that.
var a = 5 * 2; // 10
var b = 0.5 + 4.5; // 5
var c = a + b - 6.5; // 8.5
var d = (a - 5) * (b - 3); // 10
var e = a - 5 * b - 3 // -18
The Math object
There's an object in javascript called Math. It's a static object that contains lots of properties and methods for doing many types of math operations. Everything from getting max and min values, rounding numbers, square root, the exact pi, get random numbers etc. Below is a list of some very common math properties.
Math.round(2.6); // 3
Math.ceil(2.6); // 3
Math.floor(2.6); // 2
Math.abs(-5.5); // 5.5
Math.max(5, 2, 3); // 5
Math.min(5, 2, 3); // 2
Math.pow(4, 3); // 64
Math.sqrt(9); // 3
Math.random(); // Random number between 0-1
Math.floor(Math.random() * 6) + 1; // Random number between 1-6
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Math.sin(2); // 0.9
Math.cos(2); // -0.4
Math.tan(2); // -2.2
Math.exp(4); // 54.6
Math.log(10); // 2.3
Infinity and NaN
If a number in an operation results in a number greater than javascripts biggest, there won't be any error thrown. Instead the value results in infinity. The same thing happens if it's too small, except it then results in minus infinity (-Infinity).
NaN is a special number. It's the result when dividing 0 with 0, infinity with infinity, square root from a negative number or trying to convert non numeric strings to numbers. Why anyone would want to do that is a question, but the result is then NaN which means not a number.
Modulu operator
Modulo is a special operator that sometimes can be a bit hard to understand. What it does is an integer division between two numbers and returns what's left. It's very useful and can be used for a lot of things, like for instance finding out if a number is even or odd.
var a = 4 % 3; // 1
var b = 10 % 3; // 1
var c = 4 % 2; // 0
From strings to numbers
Sometimes you find yourself with a number inside a string that you want to do calculations with. Then the number must be converted into a number. To do this there's a couple options. Either the parseInt or parseFloat operators can be used, or simply just the + operator which also does the trick.
For it to work the string must contain a valid number, or the result will be NaN. To later convert back to strings if wanted the toString() method can be used.
var str = '34';
typeof str; // string
var num = +str;
typeof num; // number
var str2 = num.toString();
typeof str2; // string
Try it yourself
- Try out the different math operators in javascript like addition, subtraction etc.
- Learn to know the math object and use some different functions from it.
- Try out the modulo operator.