JavaScript Numbers
- Previous Page JS String Templates
- Next Page JS BigInt
JavaScript has only one number type.
Whether to write a decimal point when writing a number is optional.
JavaScript Number
JavaScript numbers can be written with or without a decimal point:
Example
var x = 3.14; // A number with a decimal point var y = 3; // A number without a decimal point
Very large or very small numbers can be written in scientific notation:
Example
var x = 123e5; // 12300000 var y = 123e-5; // 0.00123
JavaScript numbers are always 64-bit floating-point numbers
Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, shorts, longs, floats, etc.
JavaScript numbers are always stored as double-precision floating-point numbers, according to the international IEEE 754 standard.
This format stores numbers with 64 bits, where 0 to 51 store the number (fraction), 52 to 62 store the exponent, and 63 bits store the sign:
Value (also known as Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits(0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Precision
Integers (not using exponent or scientific notation) are precise to 15 digits.
Example
var x = 999999999999999; // x will be 999999999999999 var y = 9999999999999999; // y will be 10000000000000000
The maximum number of decimal places is 17, but floating-point arithmetic is not always 100% accurate:
Example
var x = 0.2 + 0.1; // x will be 0.30000000000000004
Using multiplication and division can help solve the above problem:
Example
var x = (0.2 * 10 + 0.1 * 10) / 10; // x will be 0.3
Adding a number and a string
Warning!!
JavaScript's addition and concatenation (concatenation) both use the + operator.
Numbers use addition. Strings use concatenation.
If you add two numbers, the result will be a number:
Example
var x = 10; var y = 20; var z = x + y; // z will be 30 (a number)
If you add two strings, the result will be a string concatenation:
Example
var x = "10"; var y = "20"; var z = x + y; // z will be 1020 (string)
If you add a number and a string, the result will also be a string concatenation:
Example
var x = 10; var y = "20"; var z = x + y; // z will be 1020 (a string)
If you add a string and a number, the result will also be string concatenation:
Example
var x = "10"; var y = 20; var z = x + y; // z will be 1020 (string)
A common mistake is to think the result should be 30:
Example
var x = 10; var y = 20; var z = "The result is: " + x + y;
A common mistake is to think the result should be 102030:
Example
var x = 10; var y = 20; var z = "30"; var result = x + y + z;
JavaScript compiles from left to right.
Because x and y are both numbers, 10 + 20 will be added.
Because z is a string, 30 + "30" is concatenated.
Numeric string
JavaScript strings can contain numeric content:
var x = 100; // x is a number var y = "100"; // y is a string
In all numeric operations, JavaScript will try to convert strings to numbers:
This example runs as follows:
var x = "100"; var y = "10"; var z = x / y; // z will be 10
This example will also run as follows:
var x = "100"; var y = "10"; var z = x * y; // z will be 1000
This example runs as follows:
var x = "100"; var y = "10"; var z = x - y; // z will be 90
But this example will not run as expected:
var x = "100"; var y = "10"; var z = x + y; // z will not be 110 (but 10010)
In the last example, JavaScript concatenated strings using the + operator.
NaN - Not a Number
NaN
It is a reserved word in JavaScript, indicating that a number is not a valid number.
Trying to divide by a non-numeric string will result in NaN (Not a Number):
Example
var x = 100 / "Apple"; // x will be NaN (Not a Number)
However, if the string contains a number, the result will be a number:
Example
var x = 100 / "10"; // x will be 10
You can use the global JavaScript function isNaN()
to determine if a value is a number:
Example
var x = 100 / "Apple"; isNaN(x); // Returns true because x is not a number
in mathematical operations. NaN
.Be careful if you use NaN
then the result will also be NaN
:
Example
var x = NaN; var y = 5; var z = x + y; // z will be NaN
The result may be a concatenation:
Example
var x = NaN; var y = "5"; var z = x + y; // z will be NaN5
NaN
is a number,typeof NaN
returns number
:
Example
typeof NaN; // Returns "number"
Infinity
Infinity
(or -Infinity
)is the value returned by JavaScript when it exceeds the maximum possible number range during calculation.
Example
var myNumber = 2; while (myNumber != Infinity) { // Execute until Infinity myNumber = myNumber * myNumber; }
Dividing by 0 (zero) will also generate Infinity
:
Example
var x = 2 / 0; // x will be Infinity var y = -2 / 0; // y will be -Infinity
Infinity
is a number:typeOf Infinity
returns number
.
Example
typeof Infinity; // Returns "number"
hexadecimal
JavaScript will interpret numbers prefixed with 0x
to interpret the numerical constants as hexadecimal.
Example
var x = 0xFF; // x will be 255.
Never write numbers with leading zeros (like 07).
Some JavaScript versions interpret numbers with leading zeros as octal.
By default, JavaScript displays numbers as decimal numbers.
However, you can use toString()
The method outputs the number in hexadecimal, octal, or binary.
Example
var myNumber = 128; myNumber.toString(16); // Returns 80 myNumber.toString(8); // Returns 200 myNumber.toString(2); // Returns 10000000
Numbers can also be objects
JavaScript numbers are usually created through literals: var x = 123
But it can also be done through the keyword new
Defined as an object: var y = new Number(123)
Example
var x = 123; var y = new Number(123); // typeof x returns number // typeof y returns object
Please do not create number objects. This will slow down the execution speed.
new
The keyword complicates the code and produces some unexpected results:
When using ==
The numbers look equal when using the equality operator:
Example
var x = 500; var y = new Number(500); // (x == y) is true because x and y have equal values
When using ===
After the equality operator, the numbers that are equal become unequal because ===
Operators need both type and value to be equal.
Example
var x = 500; var y = new Number(500); // (x === y) is false because x and y are of different types
Even worse. Objects cannot be compared:
Example
var x = new Number(500); var y = new Number(500); // (x == y) is false because objects cannot be compared
JavaScript objects cannot be compared.
- Previous Page JS String Templates
- Next Page JS BigInt