JavaScript Number Properties

What are JavaScript number properties?

JavaScript number properties represent important constants such as the maximum and minimum values a number can have, special values like NaN (Not-a-Number) and infinity representations. These properties are static, meaning they are accessed directly on the Number object and not on an instance.

List of JavaScript Number Properties

  1. Number.MAX_VALUE
  2. Number.MIN_VALUE
  3. Number.NaN
  4. Number.POSITIVE_INFINITY
  5. Number.NEGATIVE_INFINITY
  6. Number.EPSILON
  7. Number.MIN_SAFE_INTEGER
  8. Number.MAX_SAFE_INTEGER

1. Number.MAX_VALUE

The MAX_VALUE property represents the largest positive number JavaScript can handle.

Value

The value of Number.MAX_VALUE is approximately 1.7976931348623157 × 10³⁰⁸.

Usage

console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308

// Checking if a number exceeds MAX_VALUE
let bigNum = 1e309;
console.log(bigNum > Number.MAX_VALUE); // Output: true
console.log(bigNum); // Output: Infinity

2. Number.MIN_VALUE

The MIN_VALUE property represents the smallest positive number JavaScript can handle (close to zero).

Value

The value of Number.MIN_VALUE is approximately 5 × 10⁻³²⁴.

Usage

console.log(Number.MIN_VALUE); // Output: 5e-324

// Checking if a number is smaller than MIN_VALUE
let smallNum = 1e-325;
console.log(smallNum < Number.MIN_VALUE); // Output: false
console.log(smallNum); // Output: 0

3. Number.NaN

The NaN property stands for “Not-a-Number” and indicates an invalid number.

Usage

console.log(Number.NaN); // Output: NaN

// Checking NaN
let invalidNum = Number("abc");
console.log(invalidNum); // Output: NaN
console.log(isNaN(invalidNum)); // Output: true

4. Number.POSITIVE_INFINITY

The POSITIVE_INFINITY property represents infinity when a positive number exceeds Number.MAX_VALUE.

Usage

console.log(Number.POSITIVE_INFINITY); // Output: Infinity

// Generating positive infinity
let inf = 1 / 0;
console.log(inf === Number.POSITIVE_INFINITY); // Output: true

5. Number.NEGATIVE_INFINITY

The NEGATIVE_INFINITY property represents negative infinity when a negative number exceeds the lowest possible value.

Usage

console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity

// Generating negative infinity
let negInf = -1 / 0;
console.log(negInf === Number.NEGATIVE_INFINITY); // Output: true

6. Number.EPSILON

The EPSILON property represents the smallest interval between two representable numbers. It helps in dealing with floating-point arithmetic issues.

Value

The value of Number.EPSILON is approximately 2.220446049250313e-16.

Usage

let a = 0.1 + 0.2;
let b = 0.3;

// Checking if the difference is within EPSILON
console.log(Math.abs(a - b) < Number.EPSILON); // Output: true

7. Number.MIN_SAFE_INTEGER

The MIN_SAFE_INTEGER property represents the minimum safe integer JavaScript can handle without precision loss.

Value

The value of Number.MIN_SAFE_INTEGER is -9007199254740991.

Usage

console.log(Number.MIN_SAFE_INTEGER); // Output: -9007199254740991

let unsafeNum = Number.MIN_SAFE_INTEGER - 1;
console.log(unsafeNum === Number.MIN_SAFE_INTEGER); // Output: true (precision lost)

8. Number.MAX_SAFE_INTEGER

The MAX_SAFE_INTEGER property represents the maximum safe integer JavaScript can handle without precision loss.

Value

The value of Number.MAX_SAFE_INTEGER is 9007199254740991.

Usage

console.log(Number.MAX_SAFE_INTEGER); // Output: 9007199254740991

let unsafeNum = Number.MAX_SAFE_INTEGER + 1;
console.log(unsafeNum === Number.MAX_SAFE_INTEGER); // Output: true (precision lost)

Combining Number Properties

You can use Number properties to build robust validations in your code.

Example

function validateNumber(num) {
if (num > Number.MAX_VALUE) {
return "Value exceeds maximum limit.";
} else if (num < Number.MIN_VALUE && num !== 0) {
return "Value is too small.";
} else if (!Number.isFinite(num)) {
return "Value is infinity.";
} else {
return "Valid number.";
}
}

console.log(validateNumber(1e309)); // Output: "Value exceeds maximum limit."
console.log(validateNumber(1e-325)); // Output: "Value is too small."
console.log(validateNumber(100)); // Output: "Valid number."

Leave a Comment