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
Example:
Javascript
<p id= "demo" ></p> <script> let x = 5; // Declaration document.getElementById( "demo" ).innerHTML = x; </script> |
Output:
5
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; var x = 0562 // x will be 370(parsed as an octal number).
If the number starts with 0 and the following number is smaller than 8. It will be parsed as an Octal Number. 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 1:
Javascript
<p id= "num" ></p> <script> var x = 0.22 + 0.12; document.getElementById( "num" ).innerHTML = "0.22 + 0.12 = " + x; </script> |
Output:
0.22 + 0.12 = 0.33999999999999997
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
Example 2:
Javascript
<p id= "num" ></p> <script> var x = 0B0111; document.getElementById( "num" ).innerHTML = "0B0111 will be " + x; </script> |
Output:
0B0111 will be 7
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 = 0111; // x will be 73 var x = 07123; // x will be 3667
Example 3:
Javascript
<p id= "num" ></p> <script> var x = 07123; document.getElementById( "num" ).innerHTML = "07123 will be " + x; </script> |
Output:
07123 will be 3667
Hexadecimal Numbers: They start with 0x or 0X followed by any digit belonging (0123456789ABCDEF)
var x = 0xfff; // x will be 4095
Example 2:
Javascript
<p id= 'num' ></p> <script> var x = 0xfff; document.getElementById( "num" ).innerHTML = "0xfff will be " + x; </script> |
Output:
0xfff will be 4095
Exponentiation:
var x = 2E5 // x will be 200000 var x = 34e3 // x will be 34000 var x = 23e-5 // x will be 0.00023
JavaScript uses the + operator for both addition and concatenation.
Addition:
var a = 20; var b = 20; var c = a + b; // c will be 40 (a number)
Concatenation:
var a = "10"; var b = "20"; var c = a + b; // c will be 1020 (a string)
Numeric Strings:
- In javascript, strings can take numeric values as well. It converts strings to numbers in every operation.
- NaN is a reserved word indicating not a number.
var x= "12345" ; // x will be a string(NaN) var x = 67 ; // x will be number
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.
Reference: https://www.geeksforgeeks.org/javascript-number-complete-reference/
Please Login to comment...