Understanding JavaScript Numbers: Types and Formats Explained
Learn about the different types of numbers in JavaScript, including integers, floats, binary, hexadecimal, octal, and exponential formats. Understand how JavaScript represents numbers using the IEEE 754 standard and explore how these numeric types are used in web development for accurate calculations and data manipulation.
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 a double-precision 64-bit binary format, following the IEEE 754 standard.
Example: Numbers in JavaScript
Example Code
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 and are accurate up to 15 digits. For integers larger than 15 digits, use BigInt
.
Example Code
// 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 BigInt
for large integers with more than 15 digits. Append n
to the end of an integer to make it BigInt
.
Example Code
// 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
Floating-point numbers in JavaScript can keep up to 17 decimal places of precision; beyond that, the value may be changed.
Example Code
// 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
Arithmetic operations on floating-point numbers are not always accurate. For example:
Example Code
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 of Numeric Strings
Arithmetic operations with numeric strings will result in a number:
Example Code
var numStr1 = "5", numStr2 = "4";
var multiplication = numStr1 * numStr2; // returns 20
var division = numStr1 / numStr2; // returns 1.25
var modulus = numStr1 % numStr2; // returns 1
Arithmetic Operation on Number and String
If one value is a number and the other is a string, the result may vary:
Example Code
var num = 5, str = "4";
var multiplication = num * str; // returns 20
var division = num / str; // returns 1.25
var modulus = num % str; // returns 1
var result = num + str; // returns "54"
Binary, Octal, Hexadecimal, Exponential
JavaScript supports different numeric formats:
- Binary: Must start with
0b
or0B
. - Octal: Must start with
0o
or0O
. - Hexadecimal: Must start with
0x
or0X
. - Exponential: Follows the format
bNeN
, whereb
is a base number followed bye
andN
is the exponent.
Example Code
var b = 0b100; // binary
var oct = 0o544; // octal
var hex = 0x123456789ABCDEF; // hexadecimal
var exp = 256e-5; // exponential
Number() Function in JavaScript
The Number()
function converts values of other types to numbers:
Example Code
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"
Number Object
Using the new
operator with Number()
returns an object containing constants and methods for working with numbers:
Example Code
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
Comparing numbers using ==
or ===
operators:
Example Code
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
JavaScript includes some default properties for the 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 ) |
Example Code
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 methods are available for the Number
type:
Method | Description |
---|---|
toExponential(fractionDigits) |
Returns the exponential value as a string. |
toFixed(fractionDigits) |
Returns a string of the number with a specified number of decimal places. |
toLocaleString() |
Returns a number as a string according to locale settings. |
toPrecision(precisionNumber) |
Returns the number as a string with a specified total number of digits. |
toString() |
Returns the string representation of the number value. |
valueOf() |
Returns the value of the number object. |
Example Code
var num = 100;
console.log(num.toExponential(2)); // returns '1.00e+2'
console.log(num.toFixed(2)); // returns '100.00'
console.log(num.toLocaleString()); // returns '100'
console.log(num.toPrecision(4)); // returns '100.0'
console.log(num.toString()); // returns '100'
var numObj = new Number(100);
console.log(numObj.valueOf()); // returns 100