JavaScript Numbers
JavaScript has only one type of number. Numbers can be written with or without decimals.
Example
var x = 3.14; // A number with decimalsvar y = 3; // A number without decimals
Example
var x = 123e5; // 12300000var y = 123e-5; // 0.00123
JavaScript Numbers are Always 64-bit Floating Point
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Precision
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.Example
var x = 999999999999999; // x will be 999999999999999var y = 9999999999999999; // y will be 10000000000000000
Example
var x = 0.2 + 0.1; // x will be 0.30000000000000004
Example
var x = (0.2 * 10 + 0.1 * 10) / 10; // x will be 0.3
Adding Numbers and Strings
WARNING !!
JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
Example
var x = 10;
var y = 20;
var z = x + y; // z will be 30 (a number)
Example
var x = "10";
var y = "20";
var z = x + y; // z will be 1020 (a string)
Example
var x = 10;
var y = "20";
var z = x + y; // z will be 1020 (a string)
Example
var x = "10";
var y = 20;
var z = x + y; // z will be 1020 (a string)
Example
var x = 10;
var y = 20;
var z = "The result is: " + x + y;
Example
var x = 10;
var y = 20;
var z = "30";
var result = x + y + z;
The JavaScript compiler works from left to right.
First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.
First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.
Numeric Strings
JavaScript strings can have numeric content:var x = 100; // x is a number
var y = "100"; // y is a string
This will work:
var x = "100";
var y = "10";
var z = x / y; // z will be 10
var x = "100";
var y = "10";
var z = x * y; // z will be 1000
var x = "100";
var y = "10";
var z = x - y; // z will be 90
var x = "100";
var y = "10";
var z = x + y; // z will not be 110 (It will be 10010)
In the last example JavaScript uses the + operator to concatenate the strings.
NaN - Not a Number
NaN is a JavaScript reserved word indicating that a number is not a legal number.Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
Example
var x = 100 / "Apple"; // x will be NaN (Not a Number)
Example
var x = 100 / "10"; // x will be 10
Example
var x = 100 / "Apple";
isNaN(x); // returns true because x is Not a Number
Example
var x = NaN;
var y = 5;
var z = x + y; // z will be NaN
Example
var x = NaN;
var y = "5";
var z = x + y; // z will be NaN5
Example
typeof NaN; // returns "number"
Infinity
Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.Example
var myNumber = 2;
while (myNumber != Infinity) { // Execute until Infinity myNumber = myNumber * myNumber;
}
Example
var x = 2 / 0; // x will be Infinityvar y = -2 / 0; // y will be -Infinity
Example
typeof Infinity; // returns "number"
Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.Example
var x = 0xFF; // x will be 255
Never write a number with a leading zero (like 07).
Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
By default, JavaScript displays numbers as base 10 decimals.Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).
Example
var myNumber = 128;
myNumber.toString(16); // returns 80myNumber.toString(8); // returns 200myNumber.toString(2); // returns 10000000
Numbers Can be Objects
Normally JavaScript numbers are primitive values created from literals:var x = 123;
But numbers can also be defined as objects with the keyword new:
var y = new Number(123);
Example
var x = 123;
var y = new Number(123);
// typeof x returns number // typeof y returns object
Do not create Number objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:
When using the == operator, equal numbers are equal:The new keyword complicates the code. This can produce some unexpected results:
Example
var x = 500;
var y = new Number(500);
// (x == y) is true because x and y have equal values
Example
var x = 500;
var y = new Number(500);
// (x === y) is false because x and y have different types
Example
var x = new Number(500);
var y = new Number(500);
// (x == y) is false because objects cannot be compared
Note the difference between (x==y) and (x===y).
Comparing two JavaScript objects will always return false.
Comparing two JavaScript objects will always return false.