In this tutorial, you'll learn about various number properties in JavaScript.
JavaScript Number.EPSILON
Number.EPSILON
represents the smallest possible difference between 1
and the next larger floating-point number.
const epsilon = Number.EPSILON;
console.log(epsilon);
// Output: 2.220446049250313e-16
Note: Number.EPSILON
is an ES6 feature and is not supported in older browsers like Internet Explorer.
JavaScript MAX_VALUE
Number.MAX_VALUE
holds the largest numerical value representable in JavaScript.
const maxValue = Number.MAX_VALUE;
console.log(maxValue);
// Output: 1.7976931348623157e+308
Important: Number properties cannot be used on variables.
let num = 6;
console.log(num.MAX_VALUE);
// Output: undefined
JavaScript MIN_VALUE
Number.MIN_VALUE
represents the smallest positive number in JavaScript.
const minValue = Number.MIN_VALUE;
console.log(minValue);
// Output: 5e-324
JavaScript MAX_SAFE_INTEGER
Number.MAX_SAFE_INTEGER
is the highest integer that JavaScript can accurately represent (2^53 - 1)
.
const maxSafeInt = Number.MAX_SAFE_INTEGER;
console.log(maxSafeInt);
// Output: 9007199254740991
JavaScript MIN_SAFE_INTEGER
Number.MIN_SAFE_INTEGER
is the smallest reliably representable integer.
const minSafeInt = Number.MIN_SAFE_INTEGER;
console.log(minSafeInt);
// Output: -9007199254740991
Note: These properties were introduced in ECMAScript 2015 (ES6) and are not available in older browsers.
JavaScript POSITIVE_INFINITY
Number.POSITIVE_INFINITY
represents a mathematical infinity.
const positiveInfinity = Number.POSITIVE_INFINITY;
console.log(positiveInfinity);
// Output: Infinity
Example: Overflowing a number calculation.
const result = 1 / 0;
console.log(result);
// Output: Infinity
JavaScript NEGATIVE_INFINITY
Number.NEGATIVE_INFINITY
represents an infinitely small value.
const negInfinity = Number.NEGATIVE_INFINITY;
console.log(negInfinity);
// Output: -Infinity
Example: Underflowing a numerical computation.
const result = -1 / 0;
console.log(result);
// Output: -Infinity
JavaScript NaN (Not-a-Number)
NaN
is used to represent an invalid number.
const invalidNumber = Number.NaN;
console.log(invalidNumber);
// Output: NaN
Example: Arithmetic operations with non-numeric values return NaN
.
const result = 100 / "Apple";
console.log(result);
// Output: NaN
By understanding these Number
properties, developers can better handle numeric values, detect overflows, and ensure precision in JavaScript applications.