JavaScript Number MAX_VALUE Property
The JavaScript Number MAX_VALUE property returns the maximum numeric value possible in JavaScript. If a value is larger than the MAX_VALUE, it will be represented as 'infinity' and lose its actual numeric value.
It is a static property of the Number object. You always use it as a "Number.MAX_VALUE", rather than as a property of a number value. If you use x.MAX_VALUE, where 'x' is a variable, it will return 'undefined'.
Syntax
Following is the syntax of JavaScript Number MAX_VALUE property −
Number.MAX_VALUE
Parameters
- It does not accept any parameters.
Return value
It returns the maximum numeric value representable in JavaScript i.e "1.7976931348623157E+308".
Example 1
The following example demonstrates the usage of the JavaScript Number MAX_VALUE property.
<html>
<head>
<title>JavaScript MAX_VALUE</title>
</head>
<body>
<script>
document.write("MAX_VALUE = ", Number.MAX_VALUE);
</script>
</body>
</html>
Output
The above program returns the max value as "1.7976931348623157e+308".
MAX_VALUE = 1.7976931348623157e+308
Example 2
If you try to access the MAX_VALUE property using a variable (e.g. x.MAX_VALUE), the output would be "undefined".
<html>
<head>
<title>JavaScript MAX_VALUE</title>
</head>
<body>
<script>
let x = 20;
document.write("Variable value = ", x);
document.write("<br>MAX_VALUE = ", x.MAX_VALUE);
</script>
</body>
</html>
Output
Once the above program is executed, it will return an 'undefined'.
Variable value = 20 MAX_VALUE = undefined
Example 3
If the value is larger than the possible MAX_VALUE, the value will be represented as "infinity" in the output and will loose its actual value.
<html>
<head>
<title>JavaScript MAX_VALUE</title>
</head>
<body>
<script>
function multi(a, b){
if((a * b)> Number.MAX_VALUE){
return "Infinity....!";
}
else{
return a * b;
}
}
var m_val = 1.7976931348623157E+308;
var n1 = 1;
var n2 = 2;
document.write("value of ", m_val , " x ", n1 , " = ", multi(m_val, n1));
document.write("<br>value of ", m_val , " x ", n2 , " = ", multi(m_val, n2));
</script>
</body>
</html>
Output
After executing the above program, it will return an "Infinity".
value of 1.7976931348623157e+308 x 1 = 1.7976931348623157e+308 value of 1.7976931348623157e+308 x 2 = Infinity....!