In JavaScript, numbers are primitive data types. For example,
const a = 3;
const b = 3.13;
Unlike in some other programming languages, you don't have to specifically declare for integer or floating values using int, float, etc.
You can use exponential notation e to include too large or too small numbers. For example,
const a1 = 5e9;
console.log(a1); //5000000000
const a2 = 5e-5;
console.log(a2); // 0.00005
Numbers can also be denoted in hexadecimal notation. For example,
const a = 0xff;
console.log(a); // 255
const b = 0x00 ;
console.log(b); // 0
+ Operator with Numbers
When +
is used with numbers, it is used to add the numbers. For example,
const a = 4 + 9;
console.log(a); // 13
When +
is used with numbers and strings, it is used to concatenate them. For example,
const a = '4' + 9;
console.log(a); // 49
When a numeric string is used with other numeric operations, the numeric string is converted to a number. For example,
const a = '4' - 2;
console.log(a); // 2
const a = '4' / 2;
console.log(a); // 2
const a = '4' * 2;
console.log(a); // 8
JavaScript NaN
In JavaScript, NaN
(Not a Number) is a keyword that indicates that the value is not a number.
Performing arithmetic operations (except +
) to numeric value with string results in NaN
. For example,
const a = 4 - 'hello';
console.log(a); // NaN
The built-in function isNaN()
can be used to find if a value is a number. For example,
const a = isNaN(9);
console.log(a); // false
const a = isNaN(4 - 'hello');
console.log(a); // true
When the typeof
operator is used for NaN
value, it gives a number output. For example,
const a = 4 - 'hello';
console.log(a); // NaN
console.log(typeof a); // "number"
JavaScript Infinity
In JavaScript, when calculation is done that exceeds the largest (or smallest) possible number, Infinity
(or -Infinity
) is returned. For example,
const a = 2 / 0;
console.log(a); // Infinity
const a = -2 / 0;
console.log(a); // -Infinity
JavaScript BigInt
In JavaScript, Number type can only represent numbers less than (253 - 1) and more than -(253 - 1). However, if you need to use a larger number than that, you can use the BigInt data type.
A BigInt number is created by appending n to the end of an integer. For example,
// BigInt value
const value = 900719925124740998n;
// Adding two big integers
const value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"
Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers. Visit JavaScript BigInt support to learn more.
JavaScript Numbers Are Stored in 64-bit
In JavaScript, numbers are stored in 64-bit format IEEE-754, also known as "double precision floating point numbers".
The numbers are stored in 64 bits (the number is stored in 0 to 51 bit positions, the exponent in 52 to 62 bit positions and the sign in 63 bit position).
Numbers | Exponent | Sign |
---|---|---|
52 bits(0 - 51) | 11 bits(52- 62) | 1 bit(63) |
Precision Problems
Operations on floating-point numbers results in some unexpected results. For example,
const a = 0.1 + 0.2;
console.log(a); // 0.30000000000000004
The result should be 0.3 instead of 0.30000000000000004. This error occurs because in JavaScript, numbers are stored in binary form to represent decimal digits internally. And decimal numbers can't be represented in binary form exactly.
To solve the above problem, you can do something like this:
const a = (0.1 * 10 + 0.2 * 10) / 10;
console.log(a); // 0.3
You can also use the toFixed()
method.
const a = 0.1 + 0.2;
console.log(a.toFixed(2)); // 0.30
toFixed(2)
rounds up the decimal number to two decimal values.
const a = 9999999999999999
console.log(a); // 10000000000000000
Note: Integers are accurate up to 15 digits.
Number Objects
You can also create numbers using the new
keyword. For example,
const a = 45;
// creating a number object
const b = new Number(45);
console.log(a); // 45
console.log(b); // 45
console.log(typeof a); // "number"
console.log(typeof b); // "object"
Note: It is recommended to avoid using number objects. Using number objects slows down the program.
JavaScript Number Methods
Here is a list of built-in number methods in JavaScript.
Method | Description |
---|---|
isNaN() | determines whether the passed value is NaN |
isFinite() | determines whether the passed value is a finite number |
isInteger() | determines whether the passed value is an integer |
isSafeInteger() | determines whether the passed value is a safe integer |
parseFloat(string) | converts the numeric floating string to floating-point number |
parseInt(string, [radix]) | converts the numeric string to integer |
toExponential(fractionDigits) | returns a string value for a number in exponential notation |
toFixed(digits) | returns a string value for a number in fixed-point notation |
toPrecision() | returns a string value for a number to a specified precision |
toString([radix]) | returns a string value in a specified radix(base) |
valueof() | returns the numbers value |
toLocaleString() | returns a string with a language sensitive representation of a number |
For example,
// check if a is integer
const a = 12;
console.log(Number.isInteger(a)); // true
// check if b is NaN
const b = NaN;
console.log(Number.isNaN(b)); // true
// display upto two decimal point
const d = 5.1234;
console.log(d.toFixed(2)); // 5.12
JavaScript Number Properties
Here is a list of Number properties in JavaScript.
Property | Description |
---|---|
EPSILON | returns the smallest interval between two representable numbers |
MAX_SAFE_INTEGER | returns the maximum safe integer |
MAX_VALUE | returns the largest possible value |
MIN_SAFE_INTEGER | returns the minimum safe integer |
MIN_VALUE | returns the smallest possible value |
NaN | represents 'Not-a-Number' value |
NEGATIVE_INFINITY | represents negative infinity |
POSITIVE_INFINITY | represents positive infinity |
prototype | allows the addition of properties to Number objects |
For example,
// largest possible value
const a = Number.MAX_VALUE;
console.log(a); // 1.7976931348623157e+308
// maximum safe integer
const a = Number.MAX_SAFE_INTEGER;
console.log(a); // 9007199254740991
JavaScript Number() Function
The Number()
function is used to convert various data types to numbers. For example,
const a = '23'; // string
const b = true; // boolean
//converting to number
const result1 = Number(a);
const result2 = Number(b);
console.log(result1); // 23
console.log(result2); // 1
If you want to learn more about the number conversion, visit JavaScript Type Conversion.