JavaScript MAX_VALUE and MIN_VALUE Properties
This tutorial explains the MAX_VALUE and MIN_VALUE properties of JavaScript's Number object. Both properties are constant (or read-only: their value cannot be changed by your scripts.)
The MAX_VALUE property represents the largest numerical value the JavaScript interpreter can handle. The MIN_VALUE property corresponds to the smallest value (closest to zero) the JavaScript interpreter can handle.
MAX_VALUE vs. POSITIVE_INFINITY — The value of the MAX_VALUE property is the largest number your JavaScript interpreter can handle. Larger value will be viewed as POSITIVE_INFINITY.
MIN_VALUE vs. NEGATIVE_INFINITY — The value of the MIN_VALUE property is the smallest (closest to zero) number your JavaScript interpreter can handle, while NEGATIVE_INFINITY is the largest negative number the JavaScript interpreter can represent.
JavaScript Number Scale
The number scale below illustrate where JavaScript's number property constants fit in. It is useful to remember that MIN_VALUE is not the largest negative number: it is the smallest positive number JavaScript can represent.
Displaying the Values of the MAX_VALUE and MIN_VALUE
MAX_VALUE and MIN_VALUE are not properties of any variable of Number data type. Rather, these properties belong to the actual Number object. The code below displays the value of the MAX_VALUE property:
alert( Number.MAX_VALUE.toString() );
The toString() method simply converts the number to its string representation. The value returned is: (shown in exponential notation)
By default, the JavaScript interpreter displays extremely small and extremely large numbers in exponential notation. But we can force JavaScript to display the value of MAX_VALUE in decimal, rather than exponential notation, by calling the toFixed() method (covered in a later tutorial.)
alert( Number.MAX_VALUE.toFixed() );
Our script yields the following result:
The JavaScript code below displays the value of the MIN_VALUE property:
alert( Number.MIN_VALUE.toString() );
The result returned is shown below.
As done above for MAX_VALUE, we can force JavaScript to display MIN_VALUE in decimal, rather than exponential notation:
alert( Number.MIN_VALUE.toFixed() );
Because the number is so small, JavaScript simply returned zero:
Test the Number.MAX_VALUE Property
Interactively test the Number MAX_VALUE property by editing the JavaScript code below and clicking the Test Number MAX_VALUE Property button.
Browser support for MAX_VALUE and MIN_VALUE properties | |||
---|---|---|---|