WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Numbers


JavaScript has just a single sort of number.

Numbers can be composed with, or without, decimals.


JavaScript Numbers

JavaScript numbers can be composed with, or without decimals:

Example

var x = 34.00;   /A number with decimals
var y = 34;      /A number without decimals

Extra huge or additional little numbers can be composed with logical (example) notation:

Example

var x = 123e5;   /12300000
var y = 123e-5;  /0.00123

JavaScript Numbers are Always 64-bit Floating Point

Unlike numerous other programming dialects, JavaScript does not characterize diverse sorts of numbers, similar to whole numbers, short, long, drifting point etc.

JavaScript numbers are constantly put away as twofold exactness coasting point numbers, following the worldwide IEEE 754 standard.

This group stores numbers in 64 bits, where the number (the division) is put away in bits 0 to 51, the example in bits 52 to 62, and the sign in bit 63:

Value (otherwise known as Fraction/Mantissa) Exponent Sign
52 bits (0 - 51)  11 bits (52 - 62) 1 bit (63)

Precision

Integers (numbers without a period or example documentation) are considered exact up to 15 digits.

Example

var x = 999999999999999;  /x will be 999999999999999
var y = 9999999999999999; /y will be 10000000000000000
Try it Yourself »

The greatest number of decimals is 17, however drifting point math isn't continuously 100% accurate:

Example

var x = 0.2 + 0.1;        /x will be 0.30000000000000004
Try it yourself »

To take care of the issue above, it increases and divide:

Example

var x = (0.2 * 10 + 0.1 * 10)/10;      /x will be 0.3
Try it Yourself »

Hexadecimal

JavaScript translates numeric constants as hexadecimal on the off chance that they are gone before by 0x.

Example

var x = 0xFF;             /x will be 255
Try it Yourself »

As a matter of course, Javascript shows numbers as base 10 decimals.

But you can utilize the toString() strategy to yield numbers as base 16 (hex), base 8 (octal), or base 2 (binary).

Example

var myNumber = 128;
myNumber.toString(16);    /returns 80
myNumber.toString(8);     /returns 200
myNumber.toString(2);     /returns 10000000
Try it Yourself »

Infinity

Infinity (or - Infinity) is the esteem JavaScript will return whether you figure a number outside the biggest conceivable number.

Example

var myNumber = 2;
while (myNumber != Infinity) {          /Execute until Infinity
    myNumber = myNumber * myNumber;
}
Try it yourself »

Division by 0 (zero) likewise creates Infinity:

Example

var x =  2/0;          /x will be Infinity
var y = - 2/0;          /y will be - Infinity
Try it Yourself »

Infinity is a number: typeOf Infinity returns number.

Example

typeof Infinity;       /returns "number"
Try it Yourself »

NaN - Not a Number

NaN is a JavaScript held word demonstrating that an esteem isn't a number.

Trying to do number juggling with a non-numeric string will result in NaN (Not a Number):

Example

var x = 100/"Apple";  /x will be NaN (Not a Number)
Try it Yourself »

However, if the string contains a numeric esteem , the outcome will be a number:

Example

var x = 100/"10";     /x will be 10
Try it Yourself »

You can utilize the worldwide JavaScript work isNaN() to see whether an esteem is a number.

Example

var x = 100/"Apple";
isNaN(x);              /returns genuine in light of the fact that x is anything but a Number
Try it Yourself »

Watch out for NaN. In the event that you use NaN in a scientific activity, the outcome will likewise be NaN:

Example

var x = NaN;
var y = 5;
var z = x + y;         /z will be NaN
Try it Yourself »

 Or the outcome may be a link:

Example

var x = NaN;
var y = "5";
var z = x + y;         /z will be NaN5
Try it Yourself »

NaN is a number, and typeof NaN returns number:

Example

typeof NaN;            /returns "number"
Try it Yourself »

Numbers Can be Objects

Normally JavaScript numbers are crude qualities made from literals: var x = 123

But numbers can likewise be characterized as articles with the catchphrase new: var y = new Number(123)

Example

var x = 123;
var y = new Number(123);

/ typeof x returns number
/ typeof y returns object
Try it yourself »

When utilizing the == correspondence administrator, rise to numbers looks equal:

Example

var x = 500;             
var y = new Number(500);

/(x == y) is genuine in light of the fact that x and y have rise to values
Try it Yourself »

When utilizing the === balance administrator, break even with numbers are not equivalent, in light of the fact that the === administrator anticipates uniformity in both sort and value.

Example

var x = 500;             
var y = new Number(500);

/(x === y) is false on the grounds that x and y have diverse sorts
Try it Yourself »

Or much more dreadful. Articles can't be compared: