JavaScript's Number object includes several built-in properties that provide useful information about numbers in JavaScript.
These properties are static, meaning they are accessed directly from the Number object rather than instances of Number.
This tutorial will go over the most commonly used Number properties, explaining their significance and showing practical code examples.
Table of Contents
1. Number.MAX_VALUE
Description: This property represents the maximum positive value that can be represented in JavaScript. If a number is larger than this, JavaScript will return Infinity.
Value: Approximately 1.7976931348623157e+308.
Example 1: Using Number.MAX_VALUE
const maxValue = Number.MAX_VALUE; console.log(maxValue); // Output: 1.7976931348623157e+308 console.log(maxValue + 1); // Output: 1.7976931348623157e+308 (No change) console.log(maxValue * 2); // Output: Infinity
Explanation: When you add to or multiply Number.MAX_VALUE by a large enough number, JavaScript returns Infinity.
2. Number.MIN_VALUE
Description: This property represents the smallest positive number that can be represented in JavaScript. It is the closest to zero but not zero.
Value: Approximately 5e-324.
Example 2: Using Number.MIN_VALUE
const minValue = Number.MIN_VALUE; console.log(minValue); // Output: 5e-324 console.log(minValue / 2); // Output: 0 console.log(minValue > 0); // Output: true
Explanation: Dividing Number.MIN_VALUE by a number greater than 1 results in 0. However, Number.MIN_VALUE itself is still greater than zero.
3. Number.POSITIVE_INFINITY
Description: This property represents positive infinity. It is returned when a number exceeds Number.MAX_VALUE or when a positive number is divided by zero.
Value: Infinity.
Example 3: Using Number.POSITIVE_INFINITY
const positiveInfinity = Number.POSITIVE_INFINITY; console.log(positiveInfinity); // Output: Infinity console.log(1 / 0); // Output: Infinity console.log(positiveInfinity + 1000); // Output: Infinity
Explanation: Number.POSITIVE_INFINITY can be used to represent an infinite value in mathematical operations. Any arithmetic operation involving infinity (except subtraction with infinity) results in Infinity.
4. Number.NEGATIVE_INFINITY
Description: This property represents negative infinity. It is returned when a number is smaller than -Number.MAX_VALUE or when a negative number is divided by zero.
Value: -Infinity.
Example 4: Using Number.NEGATIVE_INFINITY
const negativeInfinity = Number.NEGATIVE_INFINITY; console.log(negativeInfinity); // Output: -Infinity console.log(-1 / 0); // Output: -Infinity console.log(negativeInfinity - 1000); // Output: -Infinity
Explanation: Similar to positive infinity, Number.NEGATIVE_INFINITY can represent an infinitely large negative number. Any operation involving negative infinity (except addition with infinity) results in -Infinity.
5. Number.NaN (Not-a-Number)
Description: This property represents a value that is “Not-a-Number”. It is usually the result of an invalid or undefined mathematical operation (e.g., dividing zero by zero).
Value: NaN.
Example 5: Using Number.NaN
const notANumber = Number.NaN; console.log(notANumber); // Output: NaN console.log(0 / 0); // Output: NaN console.log(Number('Hello')); // Output: NaN console.log(notANumber === NaN); // Output: false console.log(Number.isNaN(notANumber)); // Output: true
Explanation: NaN is a special value in JavaScript used to denote a non-numeric result. Note that NaN === NaN returns false, so to check if a value is NaN, you need to use Number.isNaN().
6. Number.EPSILON
Description: This property represents the smallest difference between two representable numbers in JavaScript. It is useful for comparing floating-point numbers due to precision errors.
Value: Approximately 2.220446049250313e-16.
Example 6: Using Number.EPSILON for Floating-Point Comparisons
const a = 0.1 + 0.2; const b = 0.3; console.log(a === b); // Output: false (Due to floating-point precision) console.log(Math.abs(a - b) < Number.EPSILON); // Output: true
Explanation: Floating-point arithmetic can lead to precision errors. Number.EPSILON provides a tiny threshold to compare numbers and determine if they are “close enough.”
7. Number.MAX_SAFE_INTEGER
Description: This property represents the maximum safe integer in JavaScript (2^53 – 1). Any number larger than this may lose precision.
Value: 9007199254740991.
Example 7: Using Number.MAX_SAFE_INTEGER
const maxSafeInteger = Number.MAX_SAFE_INTEGER; console.log(maxSafeInteger); // Output: 9007199254740991 console.log(maxSafeInteger + 1); // Output: 9007199254740992 console.log(maxSafeInteger + 2); // Output: 9007199254740992 (Incorrect due to precision loss)
Explanation: Once you go beyond Number.MAX_SAFE_INTEGER, JavaScript may not represent the number accurately due to the limits of its 64-bit floating-point format.
8. Number.MIN_SAFE_INTEGER
Description: This property represents the minimum safe integer in JavaScript (-(2^53 – 1)). Any number smaller than this may lose precision.
Value: -9007199254740991.
Example 8: Using Number.MIN_SAFE_INTEGER
const minSafeInteger = Number.MIN_SAFE_INTEGER; console.log(minSafeInteger); // Output: -9007199254740991 console.log(minSafeInteger - 1); // Output: -9007199254740992 console.log(minSafeInteger - 2); // Output: -9007199254740992 (Incorrect due to precision loss)
Explanation: Similar to Number.MAX_SAFE_INTEGER, numbers below Number.MIN_SAFE_INTEGER may not be accurately represented.
Summary of Number Properties
Property | Description | Example Value |
---|---|---|
Number.MAX_VALUE |
Largest positive number | 1.7976931348623157e+308 |
Number.MIN_VALUE |
Smallest positive number | 5e-324 |
Number.POSITIVE_INFINITY |
Represents positive infinity | Infinity |
Number.NEGATIVE_INFINITY |
Represents negative infinity | -Infinity |
Number.NaN |
Represents “Not-a-Number” | NaN |
Number.EPSILON |
Smallest difference between two numbers | 2.220446049250313e-16 |
Number.MAX_SAFE_INTEGER |
Largest integer safely representable | 9007199254740991 |
Number.MIN_SAFE_INTEGER |
Smallest integer safely representable | -9007199254740991 |
Conclusion
JavaScript's Number
properties provide valuable information about the numeric capabilities and limitations of the language.
Understanding these properties can help you write more robust code, especially when working with large numbers, floating-point arithmetic, or numeric precision.
Experiment with these examples to become more familiar with JavaScript's Number
properties!