Number & Math

All numbers in JavaScript, both integer or floating point are type of Number.

JavaScript Internally a store numbers by the floating-point format also called “double precision”.

JavaScript takes 8 bytes to store a number.

JavaScript store the maximum integer value is about 253.

Number’s Magic

We are able to write the number in JavaScript using the hexadecimal and octal radix:

Hexadecimal example:

var n0 = 0x00; // 0
var n1 = 0x01; // 1
var n2 = 0x02; // 2
var n3 = 0x03; // 3
var n4 = 0x04; // 4
var n5 = 0x05; // 5
var n6 = 0x06; // 6
var n7 = 0x07; // 7
var n8 = 0x08; // 8
var n9 = 0x09; // 9
var n10 = 0xA; // 10

alert(n0);  // 0
alert(n1);  // 1
alert(n2);  // 2
alert(n3);  // 3
alert(n4);  // 4
alert(n5);  // 5
alert(n6);  // 6
alert(n7);  // 7
alert(n8);  // 8
alert(n9);  // 9
alert(n10); // 10

Octal example:

var n0 = 000; // 0
var n1 = 001; // 1
var n2 = 002; // 2
var n3 = 003; // 3
var n4 = 004; // 4
var n5 = 005; // 5
var n6 = 006; // 6
var n7 = 007; // 7
var n8 = 010; // 8
var n9 = 011; // 9
var n10 = 012; // 10

alert(n0);  // 0
alert(n1);  // 1
alert(n2);  // 2
alert(n3);  // 3
alert(n4);  // 4
alert(n5);  // 5
alert(n6);  // 6
alert(n7);  // 7
alert(n8);  // 8
alert(n9);  // 9
alert(n10); // 10

Scientific Form

JavaScript also provides the scientific form to write or use the number.

Scientific number always consists of a number followed by “e“.

In scientific form many peoples called “e” the quantity of zeroes.

var n1 = 5e3;
alert(n1); // 5000

In above example I write the 5e3 means 5 will be followed by 3 zeroes.

var n1 = 5e-3;
alert(n1); // 0.005

If the quantity of zeroes is negative, then the number is shifted past the decimal point.

Zero Division

Many times while performing the arithmetic operation, we faced the division by Zero problem. And Many programming language like C# and Java throws the Division By Zero Exception to handle the division by Zero.

But in JavaScript when we divide the number with the Zero, than the JavaScript will return Infinity. Yes, that’s sound crazy, but it’s correct.

var number = 13;
var divider = 0;

var result = number / divider;

alert(result); // Infinity

If divide the negative number than it return the negative Infinity.

var number = -13;
var divider = 0;

var result = number / divider;

alert(result); // -Infinity

So be careful while divide the number with the Zero.

Infinity

Infinity is larger number than any number in the JavaScript.

alert(Infinity > 111111111111111111111111111111111111111111111111111111111111); //true

Infinity value can not be changed, If you add or subtract the number from the Infinity, it can not change the value of it.

alert(Infinity - 5 === Infinity); //true
alert(Infinity + 50 === Infinity); //true

NaN

NaN stands for the Not-A-Number.

While performing the mathematical operation sometimes we received the result NaN. Or If we perform some mathematical operation and it can not be completed successfully than it return the NaN.

NaN is also return when we are trying to convert the invalid number string into the number.

var numberString = "A9A";

var number = parseInt(numberString);

alert(number); // NaN

NaN in not equal to anything, even NaN is not equal to itself.

alert(NaN === NaN); // false
alert(NaN == NaN); // false

We must be have to use the isNaN() method to check the result is NaN or not.

alert(isNaN(NaN)); // true

var numberString = "A9999";

var result = parseInt(numberString);

alert(isNaN(result)); // true

In JavaScript, Mathematical operation can not be generate the error, In worst it gives the NaN.

parseInt()

To convert the valid number string into integer number, we can use the parseInt() method.

var string = "123";

var number = parseInt(string);

alert(number); // 123

If the string have the floating-point number than parseInt() method will remove the value after the point and return the whole integer only.

var string = "123.123";

var number = parseInt(string);

alert(number); // 123

If the string does not have the valid number like it have the character in the string than it return the NaN.

var string = "ABC123";

var number = parseInt(string);

alert(number); // NaN

parseFloat()

To convert the valid number string into the double or float, we can use the parseFloat() method.

var string = "123.456";

var number = parseFloat(string);

alert(number); // 123.456

If the string does not have the floating-point value but it has the valid integer number than it will return the Integer. Or if the string has the number with the .00 pointing value than it also return the Integer.

var string = "123.00";

var number = parseFloat(string);

alert(number); // 123

var string1 = "456";

var number1 = parseFloat(string1);

alert(number1); // 456

If the string does not have the valid number than it return the NaN.

var string = "AAA123.00";

var number = parseFloat(string);

alert(number); // NaN

Rounding

In each and every programming language we need the rounding function to round the number like conver the floating-point or double number to the integer.

JavaScript also provides the three function for it:

Math.floor()

Math.floor() function will convert the floating-point number into the nearest lower number.

var number = 5.4;

alert(Math.floor(number)); // 5

Math.ceil()

Math.ceil() function will convert the floating-point number into the nearest higher number.

var number = 5.4;

alert(Math.ceil(number)); // 6

Math.round()

Math.round() function will convert the floating-point number into the nearest higher or lower number. Math.round() will choose the vary nearest number.

alert(Math.round(5.4)); // 5
alert(Math.round(5.5)); // 6
alert(Math.round(5.6)); // 6

Negative numbers:

alert(Math.round(-5.6)); // -6
alert(Math.floor(-5.6)); // -6
alert(Math.ceil(-5.6)); // -5

Positive numbers:

alert(Math.round(5.6)); // 6
alert(Math.floor(5.6)); // 5
alert(Math.ceil(5.6)); // 6

Number rounding using Bitwise operators

Bitwise operators can simply remove decimal part of the number.

XOR ^ 0 : XOR ^ 0 does not change the number but remove the decimal part of the number.
~~ : it also shift the right side value to the Zero.
>> 0 : it also shift the right side value to the Zero.

alert(13.5 ^ 0); // 13
alert(13.1 ^ 0); // 13
alert(13.6 ^ 0); // 13

alert(~~13.5); // 13
alert(~~13.1); // 13
alert(~~13.6); // 13

alert(-13.5 ^ 0); // -13
alert(-13.1 ^ 0); // -13
alert(-13.6 ^ 0); // -13

alert(~~-13.5); // -13
alert(~~-13.1); // -13
alert(~~-13.6); // -13

toFixed()

toFixed() function can directly called on the number.

toFixed() function can round the number to given precision and return the string.

var number = 13.5;
alert(number.toFixed(0)); // "14"

var number2 = 13.5;
alert(number2.toFixed(2)); // "13.50"

var number3 = 13.05
alert(number3.toFixed(2)); // "13.05"

var number4 = 13.05
alert(number4.toFixed(2)); // "13.05"

var number5 = 13.05
alert(number5.toFixed(5)); // "13.05000"

Random numbers

Math.random() function will help us to get the random number between 0 to 1.

alert(Math.random());
alert(Math.random());

Generate random number between two numbers.

var minimum = 100, maximum = 200;

alert(Math.floor(minimum + Math.random() * (maximum - minimum)));