Please enable Javascript to correctly display the contents on Dot Net Tricks!


Posted On : 01 Apr 2015
Updated On : 01 Apr 2015
Total Views : 2783

Unlike object-oriented programming languages (C++, C#, Java), JavaScript doesn't has different types of numbers, like integer, floating-point, long, double etc. JavaScript has only one type of number that is floating-point number. You can write JavaScript numbers with or without decimal.

var x = 5; // number without decimal
var y = 5.42; // number with decimal

A JavaScript number is a 64bit (8 bytes) floating-point number which has range of 5e-324 (negative) to 1.7976931348623157e+308 (positive).

JavaScript Numbers Accuracy

JavaScript numbers without decimal are considered accurate up to 15 digits and JavaScript Numbers with decimal are considered accurate up to 16 digits.

//Non-decimal number accuracy test
var x = 999999999999999; //15 digits
var y = 9999999999999999; //16 digits 
var z = 8999999999999999; //16 digit

console.log(x); //999999999999999
console.log(y); //10000000000000000
console.log(z); //8999999999999999

//Decimal number accuracy test
var x = 1.999999999999999; //16 digits
var y = 1.9999999999999999; //17 digits
var z = 11.999999999999999; //17 digits

console.log(x); //1.999999999999999
console.log(y); //2
console.log(z); //11.999999999999998

var x = 16.99999999999999; //16 digits
var y = 161.9999999999999; //16 digits
var z = 161.99999999999999; //17 digits

console.log(x); //16.99999999999999
console.log(y); //161.9999999999999
console.log(z); //162 


If you want to get accurate result, take non-decimal number up to 15 digits and decimal numbers up to 16 digits. Since floating point arithmetic is not always 100% accurate.

JavaScript Special Type Numbers

JavaScript has two special types numbers : Infinity and NaN.

console.log(typeof(Infinity)) //Number
console.log(typeof(NaN)) //Number
  1. Infinity

    A JavaScript expression can return Infinity or -Infinity value as a result. For example, when you divide a number by 0 (zero), it will return Infinity

    console.log(5/0); //Infinity
    console.log(-5/0); //-Infinity
  2. NaN - Not a Number

    A JavaScript expression can return NaN value as a result. For example, when you divide a string by a number, it will return NaN

    console.log("Hello"/4); //NaN
    console.log("Hello"-4); //NaN


When you will use + operator with in your JavaScript expression and your expression have string values, it will work as concatenation operator and JavaScript expression will not return NaN.

console.log("Hello"+4); //Hello4

Numbers As Objects

You can also defined numbers as objects with the help of new keyword. Using number as an object is not recommended since they slow down execution speed.

var x = 4; //number
var y = new Number(4); //object

console.log(typeof(x)) //number
console.log(typeof(y)) //object
What do you think?

I hope, now you have better understanding of javascript numbers. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.