Table of Contents

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.

JavaScript:

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.

JavaScript:

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”

## Math

Math is fun, right? It is even more fun, when you can code simple program to do the hard stuff for you. So, what operations can you do with numbers? You can add, subtract, multiply, divide and get reminder from dividing (modulo). In JavaScript you can use mathematical symbols you know from school. These symbols are “+”, “-”, “*”, “/” and “%” for modulo. Other more complex operations can be done through built-in “Math” JavaScript library.

JavaScript:

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.

JavaScript:

var x = 13; x = x.toString(); console.log(x+2); // result - “132”

However, subtraction, division and modulo will work still the same because JavaScript will automatically convert the string back to number before it will perform mathematical operations.

JavaScript:

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

## Math Library

To be able to use any of the more complex mathematical methods you have to use “Math” keyword followed by dot and name of the method. If you want, for example, to get a square root of some number or to raise that number to n-th power, you have to use built-in JavaScript functions. These methods are pow() for raising number to n-th power and sqrt() for getting square root of number. pow() methods takes two parameters – first is for base number and second is for exponent.

JavaScript:

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.

JavaScript:

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.

JavaScript:

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.

JavaScript:

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.

JavaScript:

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.

JavaScript:

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.

JavaScript:

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.

### Summary

Today we discussed two types of number and mathematical operations you can do with them. We also used a “Math” built-in library to perform some more complex operations. In the next post from JavaScript 101 series we will talk about strings.

If you liked this article, then please consider subscribing.