Javascript Numbers

Numbers or integers are whole numbers or fractional numbers. Numbers can be added, subtracted, multiplied and divided, thus perform all arithmetic operations.

JavaScript Supports all numbers systems, including Binary , Octal, Decimal and Hexadecimal numbers. A number starting with 0 is bydefault octal in JavaScript. For example 010 is 8 in javascript.

var is used to declare numbers in JavaScript.

For fixed numbers, const is also used.

typeof operator can check datatype of numbers.

 var a=3;    	    // number; 
 var b=3.6;         // number with decimal; 
 var c=0xa;         // number in hexadecimal 
 var d=010;         // number in octal
 var e=0o10;        // number in octal
 var f=0b100;       // number in binary
 var g=NaN;         // not a number
 var h=Infinity;    // number more than 10308

Binary, Octal, Decimal and Hexadecimal Numbers Comparision

Here is a comparision of Binary numbers, Octal numbers, Decimal numbersand Hexadecimal numbers.

Number Binary
(2 bit)
(8 bit)
(10 bit)
(16 bit)
0 0 0 0 0
1 1 1 1 1
2 10 2 2 2
8 1000 10 8 8
10 1010 12 10 a
15 1111 17 15 f
16 10000 20 16 10

Methods of Numbers

toString()convert number to string.var x=6;
toLocalString()convert number to local string.var x=6;
toExponential()convert decimal to exponential notation.var x=6;
toPrecision(1)convert number to Precise .var x=1.23456;
toPrecision(2)convert decimal to exponential notation.var x=1.23456;
toPrecision(3)convert decimal to exponential notation.var x=1.23456;

String to Number conversion

Javascript variables can be strings or numbers. Like var x="5" is string, but var y=5 is number. But both can be used in arithmetic operations. Except addition, all arithmetic operations are possible with x and y.

Functions to convert string to number.

  • Number()
  • parseInt()
  • parseFloat()

	var x="5";		// string
	var y=5;		// number
	x+x="55";		// concat
	y+y=10;			// addition
	x+y="55"		// concat, as x is string
	x-x=0;			// subtraction
	y-y=0;			// subtraction
	x-y=0;			// subtraction
	x*x=25;			// multiplication
	y*y=25;			// multiplication
	x*y=25;			// multiplication
	x/x=1;			// division
	y/y=1;			// division
	x/y=1;			// division
	x%x=0;			// modulus
	y%y=0;			// modulus
	x%y=0;			// modulus

Number Function

Number Function can convert string to numbers. Number Function can convert both floating/decimal and non-floating/integers. But if a string contains string character like alphabets or special character, number function will return NaN.

    var a="100";
    var b="100.5";
    var c="100px";
    var d="abc100";
    Number(a)       //100
    Number(b)       //100.5
    Number(c)       //NaN - Not a Number
    Number(d)       //NaN - Not a Number

parseInt Function

parseInt Function can convert string to numbers, but non-floating/integers values only. parseInt can also convert binary, octal and hexadecimal to decimal numbers.

    var a="100";
    var b="100.5";
    var c="100px";
    var d="abc100";
    parseInt(a)       //100
    parseInt(a,2)     //4, binary number
    parseInt(a,8)     //64, octal number
    parseInt(a,16)    //256, hexadecimal number
    parseInt(b)       //100
    parseInt(c)       //100
    parseInt(d)       //NaN - Not a Number

parseFloat Function

parseFloat Function can convert string to numbers, floating/decimals and non-floating/intergers both. parseFloat can also convert binary, octal and hexadecimal to decimal numbers.

    var a="100";
    var b="100.5";
    var c="100px";
    var d="100.5px";
    var e="abc100";
    parseFloat(a)       //100
    parseFloat(b)       //100.5
    parseFloat(c)       //100
    parseFloat(d)       //100.5
    parseFloat(e)       //NaN - Not a Number

isNaN, is Not a Number Function

JavaScript isNaN function returns a boolean value. For example, isNaN("2") is false and isNaN("a") is true. Even isNaN(NaN) is also true.

    isNaN(NaN)                      // returns true
    isNaN(1)                        // returns false
    isNaN("1")                      // returns false
    isNaN("a")                      // returns true
    isNaN("2a")                     // returns true


isFinite function tells whether a number is finite or not. In JavaScript, number greater than 1e308 or 1e+308 are finite numbers. Number greater than 1e308 are Infinite, for example 2e308 and more are infinite numbers in JavaScript.

    isFinite(Infinity)             // false
    isFinite(2e308)                // false
    isFinite(1e308)                // true
    isFinite(1e307)                // true
    isFinite(123)                  // true

  1. Never starts a number with 0.
  2. Floating numbers can goes til 16th place.
  3. Binary i.e.0b100 and Octal 0o100 are not supported in all browsers.