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. Example: var num = 100; num.toExponential(2); // returns '1.00e+2' |
toFixed(fractionDigits) | Returns string of decimal value of a number based on specified fractionDigits. Example: var num = 100; num.toFixed(2); // returns '100.00' |
toLocaleString() | Returns a number as a string value according to a browser's locale settings. Example: var num = 100; num.toLocaleString(); // returns '100' |
toPrecision(precisionNumber) | Returns number as a string with specified total digits. Example: var num = 100; num.toPrecision(4); // returns '100.0' |
toString() | Returns the string representation of the number value. Example: var num = 100; num.toString(); // returns '100' |
valueOf() | Returns the value of Number object. Example: var num = new Number(100); num.valueOf(); // returns '100' |