JavaScript Numbers
Javascript Numbers are primitive data types. Unlike other programming languages, you don’t need int, float, etc to declare different numeric values. JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754.
This format stores numbers in 64 bits,
- 0-51 bit stores value(fraction)
- 52-62 bit stores exponent
- 63-bit stores sign
Number Literals: The types of number literals You can use decimal, binary, octal, and hexadecimal.
Decimal Numbers: JavaScript Numbers does not have different types of numbers(ex: int, float, long, short) which other programming languages do. It has only one type of number and it can hold both with or without decimal values.
var a=33; var b=3.3;
Octal Number: If the number starts with 0 and the following number is smaller than 8. It will be parsed as an Octal Number.
var x = 0562 // x will be 370(parsed as an octal number).
Binary Numbers: They start with 0b or 0B followed by 0’s and 1’s.
var x = 0b11; // x will be 3 var x = 0B0111; // x will be 7
Hexadecimal Numbers: They start with 0x or 0X followed by any digit belonging (0123456789ABCDEF)
var x = 0xfff; // x will be 4095
Example 1: In this example, we will print different numbers literals.
Javascript
// Decimal Numbersconsole.log(323) // Binary Numbersconsole.log(0b11);console.log(0B0111); // Hexadecimal Numbersconsole.log(0xfff); // Octal Numbersconsole.log(0562); |
Output:
323 3 7 4095 370
Integers are accurate up to 15 digits
var a = 999999999999999; // a will be 999999999999999 var b = 9999999999999999; // b will be 10000000000000000
The floating point is not 100% accurate. The maximum number of decimals is up to 17.
var x = 0.22 + 0.12; //x will be 0.33999999999999997
Example 2: In this example, we will check the output for the above inputs.
Javascript
var x = 0.22 + 0.12;console.log(x);console.log(9999999999999999) |
Output:
0.33999999999999997 10000000000000000
Example 3: In this example, we will use Number methods such as toString(), toExponential(), toPrecision(), isInteger(), and toLocaleString() method.
Javascript
var x = 21console.log(x.toString());console.log(x.toExponential());console.log(x.toPrecision(4));console.log(Number.isInteger(x));console.log(x.toLocaleString("bn-BD")); |
Output:
21 2.1e+1 21.00 true ২১
Some facts about numbers in JavaScript:
- If you add a string and number, there will be a string concatenation as an output.
- Javascript numbers which are primarily primitive values can also be defined as objects using a new keyword.
- Constants preceded by 0x are interpreted as hexadecimal in javascript.
- Javascript numbers are of base 10 by default, but we can use the toString() method to get output in the required base from base 2 to base 36.
- Apart from regular numbers, Javascript has BigInt numbers which are integers of arbitrary length. Regular integer numbers can’t safely exceed (253-1) or be less than -(253-1) that’s when BigInt serves the purpose.
We have a complete list of Javascript Number Objects methods, to check those please go through this Javascript Number Complete Reference article.


Please Login to comment...