After previous post where we finished exploration of functions, today we will take a step back to data types for a while in order to look at them more in detail. First data type, we will discuss today, are numbers.
In one of previous posts focused on data types, we took a quick look at numbers. As you know, we can divide the numbers we work with into two groups – integers and floats. Integers are whole numbers and floats are numbers with decimal point.
var x = 15; // integer var y = 163.3; // float
You can also convert number into string by using “toString()” method. To use this method write the number or variable name you want to convert followed by dot and method name. This method takes one parameter – radix – which is an integer between 2 and 36 that specifies the base to use for representing numeric values.
var x = 13; x = x.toString(); console.log(x); // result - “13” x = x.toString(2); console.log(x); // result - “1101” x = x.toString(4); console.log(x); // result - “31”
var x = 20; var y = 5; console.log(x-y); // result – 15 console.log(x*y); // result – 100 console.log(x / y); // result - 4 console.log(x % y); // result – 0 (reminder of division)
Remember that when you convert number to string and you try to add some number to it, result will string containing the original integer followed by the new one.
var x = 13; x = x.toString(); console.log(x+2); // result - “132”
var z = 52; z = z.toString(); console.log(z+89); // result - “5289” console.log(z / 3); // result – 17.333333333333332 console.log(z * 5); // result - 260 console.log(z % 6); // result – 4
console.log(Math.pow(2, 6)); // result – 64 var x = 12; var y = 15; console.log(Math.pow(x, y)); // result – 15407021574586368 console.log(Math.pow(x, 3)); // result – 1728 var z = Math.pow(x, 2); console.log(z); // result - 16384
sqrt() method takes only one parameter – number you want the square root of.
var x = 128; console.log(Math.sqrt(x)); // result – 11.313708498984761 var y = 16; y = Math.sqrt(y); console.log(y); // result – 4
Another usefull Math methods are min(), max(), floor(), ceil(), round(), random(). min() and max() methods takes from zero to x amount of arguments and returns the largest (max()) or lowest (min()) number from given arguments.
console.log(Math.min(2,4,1,15,1202,19,7)); // result – 1 console.log(Math.max(548, 32, 4, 5496623, 203, 1336, 1)); // result – 5496623
round(), floor() and ceil() methods takes number you want to round as a parameter. round() method will return the value of a number rounded to the nearest integer. If the decimal part is 5 or higher, number will be rounded up, otherwise it will be rounded down.
var x = 15.4; console.log(Math.round(x)); // result – 15 console.log(Math.round(9.7)); // result – 10
On the other hand, floor() will always round the number to integer less than or equal to a number.
var x = 15.4; console.log(Math.floor(x)); // result – 15 console.log(Math.floor(9.7)); // result – 9
ceil() method will round the number to integer greater than or equal to a number.
var x = 15.4; console.log(Math.ceil(x)); // result – 16 console.log(Math.ceil(9.7)); // result – 10
random() method is a little bit different. This method takes no parameter and returns random number from 0 to 1.
console.log(Math.random()); // result - … try it yourself
Math library also include couple properties like Pi or E (Euler’s constant). If you want to see all methods and properties “Math” library contain you can use “console.log(Math);” and print the list to console.
If you liked this article, please subscribe so you don't miss any future post.
If you'd like to support me and this blog, you can become a patron, or you can buy me a coffee 🙂