JS Number Properties

JavaScript Number Properties: A Beginner's Guide

In JavaScript, the global Number object comes with several built-in, read-only properties that provide useful information about the limits and special values of numbers. Understanding these properties is essential for handling edge cases, performing calculations safely, and writing robust code.

These properties are static, which means they are accessed directly from the Number object itself (e.g., Number.MAX_VALUE), not from a variable holding a number (e.g., myNumber.MAX_VALUE).


Key Number Properties

Here is a breakdown of the most important Number properties, what they represent, and their approximate values.

Property Description Approximate Value
Number.MAX_VALUE The largest possible numeric value representable in JavaScript. 1.7976931348623157e+308
Number.MIN_VALUE The smallest positive numeric value greater than zero. 5e-324
Number.MAX_SAFE_INTEGER The maximum integer that can be accurately represented without losing precision. 9007199254740991
Number.MIN_SAFE_INTEGER The minimum integer that can be accurately represented. -9007199254740991
Number.EPSILON The smallest difference between 1 and the next representable number. 2.220446049250313e-16
Number.POSITIVE_INFINITY A special value representing positive infinity, returned on overflow. Infinity
Number.NEGATIVE_INFINITY A special value representing negative infinity. -Infinity
Number.NaN A special value representing "Not-a-Number", from an invalid math operation. NaN

How to Use Number Properties

You can access these properties directly on the Number object to use them in your code, for example, to check if a number is within the safe range.

Using Number Properties Example

let biggestNum = Number.MAX_VALUE;
console.log(biggestNum); // 1.7976931348623157e+308

let smallestNum = Number.MIN_VALUE; console.log(smallestNum); // 5e-324

let biggestInt = Number.MAX_SAFE_INTEGER; console.log(biggestInt); // 9007199254740991

// Using a property in a condition let myNum = 9007199254740992; if (myNum > Number.MAX_SAFE_INTEGER) { console.log("This number might not be accurate!"); }


Other Important Concepts

Beyond the static properties, here are a few other key concepts related to JavaScript numbers.

Data Type (IEEE 754)

All JavaScript numbers are a single type: double-precision 64-bit floating-point values, following the international IEEE 754 standard. This means JavaScript does not have separate types for integers (int) and decimals (float) like other languages.

Integer Precision

Because of the floating-point representation, integers are only accurate up to 15 digits. Any integer larger than Number.MAX_SAFE_INTEGER may lose precision.

BigInt

For working with integers of arbitrary length (beyond the safe integer limits), JavaScript provides the BigInt type. You can create a BigInt by appending n to the end of an integer.

The Number.prototype Property

The Number.prototype property is a special object that allows developers to add custom properties and methods to the Number object. Any method added to the prototype will then be available to all number instances in your code. [1] This is an advanced feature used for extending JavaScript's built-in functionality.


Exercise

?

Which property would you use to check if an integer is too large to be represented accurately?