JavaScript has only one type of number. Numbers can be written with or without decimals.
Example
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
Code#00:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <title>JavaScript Numbers</title> <body> <h2>JavaScript Numbers</h2> <p>Extra large or extra small numbers can be written with scientific (exponent) notation:</p> <p id="MWC"></p> <script> var x = 123e5; var y = 123e-5; document.getElementById("MWC").innerHTML = x + "<br>" + y; </script> </body> </html> |
Output:
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:
This format stores numbers in 64 bits,
- 0-51 bit stores value(fraction)
- 52-62 bit stores exponent
- 63-bit stores sign
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.
Example
var x = 3.14; // A number with decimals
var y = 3; // A number without decimals
Code#01:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <body> <h2>JavaScript Numbers</h2> <p>Numbers can be written with or without decimals:</p> <p id="MWC"></p> <script> var x = 3.14; var y = 3; document.getElementById("MWC").innerHTML = x + "<br>" + y; </script> </body> </html> |
Output:
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
Code#02:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <title>JavaScript Numbers</title> <body> <h2>JavaScript Binary Numbers</h2> <p>They start with 0b or 0B followed by 0 and 1.</p> <p id="MWC"></p> <script> var x = 0b0111; document.getElementById("MWC").innerHTML ="0b0111 will be "+ x; </script> </body> </html> |
Output:
Octal Numbers
They start with 0 followed by a number of ranges from 0-7. If any number is used it will be taken as a decimal number.
var x = 0101; //x will be 65
var x = 07124; //x will be 3668
Code#03:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <title>JavaScript Numbers</title> <body> <h2>JavaScript Octal Numbers </h2> <p id="MWC"></p> <script> var x = 07124; document.getElementById("MWC").innerHTML ="07124 will be "+ x; </script> </body> </html> |
Output:
Hexadecimal Numbers
They start with 0x or 0X followed by any digit belonging (0123456789ABCDEF)
var x = 0xfed; //x will be 4077
Code#03:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <title>JavaScript Numbers</title> <body> <h2>JavaScript Hexadecimal Numbers</h2> <p id="MWC"></p> <script> var x = 0xfed; document.getElementById("MWC").innerHTML ="0xfed will be "+ x; </script> </body> </html> |
Output: