Math min() and max() Methods
The min() and max() methods determine which number is the smallest or largest in a group of numbers. These methods accept any number of parameters:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var max = Math.max(3, 5, 3, 6);
document.writeln(max); // 6
var min = Math.min(3, 5, 3, 6);
document.writeln(min); //3
</script>
</head>
<body>
</body>
</html>
To find the maximum or the minimum value in an array, you can use the apply() method as follows:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
document.writeln(max);//8
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Math:
- The Math Object
- Math min() and max() Methods
- Math.ceil()
- Math.floor()
- Math.round()
- random() Method