In JavaScript, a Number is a built-in object representing numerical values, including integers and floating-point numbers. It also provides methods and properties for working with numbers.
Syntax
var val = new Number(number);
If you pass the number argument as a non-number argument, then the argument cannot be converted into a number. Instead, it returns NaN (Not-a-Number).
Example
let num = new Number(11)
console.log(num)
let data = new Number('Eleven')
console.log(data)
Output
[Number: 11]
[Number: NaN]
We passed the number and non-number arguments and saw the output.
Javascript Number Properties
Number.EPSILON
The Number.EPSILON property represents the difference between 1 and the smallest floating-point Number greater than 1.
You do not have to create the Number object to access this static property.
Example
let data = Math.abs(0.2 - 0.3 + 0.1);
console.log(data);
console.log(Number.EPSILON);
console.log(data < Number.EPSILON);
We have used the Javascript Math module. See the output.
2.7755575615628914e-17
2.220446049250313e-16
true
Number.MAX_VALUE
The Number.MAX_VALUE property represents the maximum numeric value representable in JavaScript.
Let’s see the Number’s Max value.
console.log(Number.MAX_VALUE);
Output
1.7976931348623157e+308
Now, let’s check with other values.
function checkInfinity(a, b) {
if (a * b > Number.MAX_VALUE) {
return ("You Got Infinity Number");
}
return (a * b);
}
console.log(checkInfinity(1.7976931348623157e+308, 1))
console.log(checkInfinity(1.7976931348623157e+308, 2))
Output
1.7976931348623157e+308
You Got Infinity Number
Number.MIN_VALUE
The Number.MIN_VALUE property describes the smallest positive numeric value representable in JavaScript.
console.log(Number.MIN_VALUE)
Output
5e-324
See the following code.
function minimumCheck(a, b) {
if (a * b < Number.MIN_VALUE) {
return "Smallest Integer"
}
return (a * b);
}
console.log(minimumCheck(5e-324, 1))
console.log(minimumCheck(-1.7976931348623157e+308, 2))
Output
5e-324
Smallest Integer
Number.NaN
The Number.NaN property represents Not-A-Number. The equivalent of Javascript NaN.
Number.prototype
The Number.prototype property represents a prototype for a Number constructor.
Number.MAX_SAFE_INTEGER
The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (253 - 1
).
For larger integers, you can consider using BigInt.
Let’s see the value of the Number.MAX_SAFE_INTEGER.
console.log(Number.MAX_SAFE_INTEGER)
Output
9007199254740991
The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillions). The reasoning behind that Number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1)
and 253 - 1
.
Number.MIN_SAFE_INTEGER
-(253 - 1)
).console.log(Number.MIN_SAFE_INTEGER)
Output
-9007199254740991
The MIN_SAFE_INTEGER constant has a value of –9007199254740991 (-9,007,199,254,740,991 or about -9 quadrillion). The reasoning behind that Number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1)
and 253 - 1
.
Javascript Number Methods
Method | Description |
---|---|
isFinite() | It checks whether the value is a finite number. |
isInteger() | It checks whether the value is an integer. |
isNaN() | It checks whether the value is a Number.NaN. |
isSafeInteger() | It checks whether the value is a safe integer. |
toExponential(x) | It converts the Number into an exponential notation. |
toFixed(x) | It formats the Number with x numbers of digits after a decimal point. |
toLocaleString() | It converts the Number into the string based on the locale settings. |
toPrecision(x) | It formats the Number to x length. |
toString() | It converts the Number to a string. |
valueOf() | It returns a primitive value of the Number. |
That’s it for this tutorial.