Skip to main content

JavaScript Number Properties

JavaScript Number Properties

JavaScript's Number object has several built-in properties that represent special numeric values and constants. Understanding these properties is important for working with numerical values and handling edge cases in your code.


Number.MAX_VALUE

Number.MAX_VALUE represents the largest positive finite number that can be represented in JavaScript. It is useful for checking whether a number is within the allowable range.


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

Number.MIN_VALUE

Number.MIN_VALUE represents the smallest positive number that is greater than zero. It is useful for comparing values that are close to zero.


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

Number.POSITIVE_INFINITY

Number.POSITIVE_INFINITY represents positive infinity. It is returned by operations that exceed the maximum representable number, such as dividing a positive number by zero.


// Example of Number.POSITIVE_INFINITY
console.log(Number.POSITIVE_INFINITY); // Output: Infinity
console.log(1 / 0); // Output: Infinity

Number.NEGATIVE_INFINITY

Number.NEGATIVE_INFINITY represents negative infinity. It is returned by operations that exceed the minimum representable number in the negative direction, such as dividing a negative number by zero.


// Example of Number.NEGATIVE_INFINITY
console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity
console.log(-1 / 0); // Output: -Infinity

Number.NaN

Number.NaN represents "Not-a-Number" (NaN), which is a special value indicating that a value is not a legal number. This is usually the result of undefined or erroneous mathematical operations.


// Example of Number.NaN
console.log(Number.NaN); // Output: NaN
console.log(0 / 0); // Output: NaN

Conclusion

JavaScript's number properties provide useful constants for handling special numeric values. Understanding these properties helps you manage edge cases and perform reliable numeric operations in your code. Leveraging these constants effectively can enhance the robustness and accuracy of your numerical calculations.

Comments