How to use Math max() method in Javascript

Description

The max() method determine which number is the largest in a group of numbers. It can accept any number of parameters:

Example


var max = Math.max(3, 5, 3, 6); 
console.log(max); // 6//  w  w w  .  ja  v  a2  s.  c o m
    
var min = Math.min(3, 5, 3, 6); 
console.log(min); //3 

The code above generates the following result.

Max/Min value for an array

To find the maximum or the minimum value in an array, you can use the apply() method as follows:


var values = [1, 2, 3, 4, 5, 6, 7, 8]; 
var max = Math.max.apply(Math, values); 
console.log(max);//8

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window