- JavaScript Numbers: Integer, Float, Binary, Exponential, Hexadecimal, Octal
- Integers
- BigInt
- Floating-point Numbers
- Binary, Octal, Hexadecimal, Exponential
- Number() Function in JavaScript
- Compare Numbers
- Number Properties
- Number Methods
- JavaScript Numbers
- Example
- Example
- JavaScript Numbers are Always 64-bit Floating Point
- Integer Precision
- Example
- Floating Precision
- Adding Numbers and Strings
- Example
- Example
- Example
- Example
- Example
- Example
- Numeric Strings
- What are integers in JavaScript?
- What are integers?
- Ranges of integers
- More blog posts on integers
- References
JavaScript Numbers: Integer, Float, Binary, Exponential, Hexadecimal, Octal
The Number is a primitive data type used for positive or negative integer, float, binary, octal, hexadecimal, and exponential values in JavaScript.
The Number type in JavaScript is double-precision 64 bit binary format like double in C# and Java. It follows the international IEEE 754 standard.
The first character in a number type must be an integer value, and it must not be enclosed in quotation marks. The following example shows the variables having different types of numbers in JavaScript.
var num1 = 100; // integer var num2 = -100; //negative integer var num3 = 10.52; // float var num4 = -10.52; //negative float var num5 = 0xfff; // hexadecimal var num6 = 256e-5; // exponential var num7 = 030; // octal var num8 = 0b0010001; // binary
Integers
Numbers can be positive or negative integers. However, integers are floating-point values in JavaScript. Integers value will be accurate up to 15 digits in JavaScript. Integers with 16 digits onwards will be changed and rounded up or down; therefore, use BigInt for integers larger than 15 digits.
//16 digit integer var int1 = 1234567890123456; //accurate //17 digit integer var int2 = 12345678901234569; //will be 12345678901234568 //16 digit integer var int3 = 9999999999999998; //will be 9999999999999998 //16 digit integer, last digit 9 var int4 = 9999999999999999; //will be 10000000000000000
BigInt
The BigInt type is a numeric primitive type that can store integers with arbitrary precision. Use the BigInt for the large integers having more than 15 digits. Append n to the end of an integer to make it BigInt.
//16 digit integer var int1 = 1234567890123459n; //will be 1234567890123459 //17 digit integer var int2 = 12345678901234569n; //will be 12345678901234569 //20 digit integer var int3 = 9999999999999999999n; //will be 9999999999999999999
Floating-point Numbers
The floating-point numbers in JavaScript can only keep 17 decimal places of precision; beyond that, the value will be changed.
//17 decimal places var f1 = 123456789012345.9; //accurate //18 decimal places var f2 = 1234567890123456.9; //will be 1234567890123457 //19 decimal places var f3 = 1234567890123456.79; //will be 1234567890123456.8
Arithmetic operations on floating-point numbers in JavaScript are not always accurate. For example:
var f1 = 5.1 + 5.2; //will be 10.3 var f2 = 10.1 + 10.2; //will be 20.299999999999997 var f3 = (10.1*100 + 10.2*100)/100; //instead of 10.1 + 10.2
Arithmetic operation (except addition) of the numeric string will result in a number, as shown below.
var numStr1 = "5", numStr2 = "4"; var multiplication = numStr1 * numStr2; //returns20 var division = numStr1 / numStr2; //returns 1.25 var modulus = numStr1 % numStr2; //returns 1
Even if one of the values is a number, the result would be the same.
var num = 5, str = "4"; var multiplication = num * str; //returns 20 var division = num / str; //returns 1.25 var modulus = num % str; //returns 1
The + operator concatenates if any one value is a literal string.
var num = 5, str = "4"; var result = num + str; //returns "54"
Binary, Octal, Hexadecimal, Exponential
The binary numbers must start with 0b or 0B followed by 0 or 1.
The octal numbers must start with zero and the lower or upper letter ‘O’, 0o or 0O .
The Hexadecimal numbers must start with zero and the lower or upper letter ‘X’, 0x or 0X .
The exponential numbers should follow the beN format where b is a base integer or float number followed by e char, and N is an exponential power number.
var b = 0b100; // binary var oct = 0o544; // octal var hex = 0x123456789ABCDEF; // hexadecimal var exp = 256e-5; // exponential
Number() Function in JavaScript
The Number() is a constructor function in JavaScript that converts values of other types to numbers.
var i = Number('100'); var f = Number('10.5'); var b = Number('0b100'); typeof(i); // returns number typeof(f); // returns number typeof(b); // returns number
By using the new operator with the Number() function will return an object which contains constants and methods for working with numbers.
var i = new Number('100'); var f = new Number('10.5'); var b = new Number('0b100'); typeof(i); // returns object typeof(f); // returns object typeof(b); // returns object
Compare Numbers
Be careful while comparing numbers using == or === operators. The == operator compares object references and not the values whereas the === operator compare values. The following example compares numbers created by different ways.
var num1 = new Number(100); var num2 = Number('100'); var num3 = 100; num1 == num2; // true num1 === num2; // false num2 == num3;//true num2 === num3; // true num1 == num3;//true num1 === num3;//false
Number Properties
The Number type includes some default properties. JavaScript treats primitive values as objects, so all the properties and methods are applicable to both literal numbers and number objects.
The following table lists all the properties of Number type.
Property | Description |
---|---|
MAX_VALUE | Returns the maximum number value supported in JavaScript |
MIN_VALUE | Returns the smallest number value supported in JavaScript |
NEGATIVE_INFINITY | Returns negative infinity (-Infinity) |
NaN | Represents a value that is not a number. |
POSITIVE_INFINITY | Represents positive infinity (Infinity). |
Number.MAX_VALUE; //1.7976931348623157e+308 Number.MIN_VALUE; //5e-324 Number.NEGATIVE_INFINITY; //-Infinity Number.POSITIVE_INFINITY; //Infinity Number.NaN;//NaN
Number Methods
The following table lists all the methods of Number type
Method | Description |
---|---|
toExponential(fractionDigits) | Returns exponential value as a string. |
JavaScript Numbers
JavaScript has only one type of number. Numbers can be written with or without decimals.
Example
Extra large or extra small numbers can be written with scientific (exponent) notation:
Example
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) |
Integer Precision
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
Example
let x = 999999999999999; // x will be 999999999999999
let y = 9999999999999999; // y will be 10000000000000000
The maximum number of decimals is 17.
Floating Precision
Floating point arithmetic is not always 100% accurate:
To solve the problem above, it helps to multiply and divide:
Adding Numbers and Strings
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:
Example
If you add two strings, the result will be a string concatenation:
Example
If you add a number and a string, the result will be a string concatenation:
Example
If you add a string and a number, the result will be a string concatenation:
Example
A common mistake is to expect this result to be 30:
Example
A common mistake is to expect this result to be 102030:
Example
The JavaScript interpreter 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.
Numeric Strings
JavaScript strings can have numeric content:
let x = 100; // x is a number
let y = «100»; // y is a string
JavaScript will try to convert strings to numbers in all numeric operations:
What are integers in JavaScript?
According to the ECMAScript specification, all numbers in JavaScript are floating-point. Yet, the notion of integer comes up occasionally. This blog post explains what it means.
What are integers?
JavaScript has only floating-point numbers. Integers appear internally in two ways. First, most JavaScript engines store a small enough number without a decimal fraction as an integer (with, for example, 31 bits) and maintain that representation as long as possible. They have to switch back to a floating point representation if a number’s magnitude grows too large or if a decimal fraction appears.
Second, the ECMAScript specification has integer operators: namely, all of the bitwise operators. Those operators convert their operands to 32-bit integers and return 32-bit integers. For the specification, integer only means that the numbers don’t have a decimal fraction, and 32-bit means that they are within a certain range. For engines, 32-bit integer means that an actual integer (non-floating-point) representation can usually be introduced or maintained.
Ranges of integers
- Safe integers [1], the largest practically usable range of integers that JavaScript supports:
- 53 bits plus a sign, range (−2 53 , 2 53 )
- 32 bits, unsigned
- Maximum length: 2 32 −1
- Range of indices: [0, 2 32 −1) (excluding the maximum length!)
- Unsigned right shift operator (>>>): 32 bits, unsigned, range [0, 2 32 )
- All other bitwise operators: 32 bits, including a sign, range [−2 31 , 2 31 )
- Accepted by String.fromCharCode()
- Returned by String.prototype.charCodeAt()
- 16 bit, unsigned
More blog posts on integers
References