Name

Math.max( ) Method — determine the larger of two numbers

Availability

Flash 5; may be used when exporting Flash 4 movies

Synopsis

Math.max(x, y)

Arguments

x

A number.

y

A number.

Returns

The larger of x and y.

Example

Math.max(5, 1);    // Returns 5
Math.max(-6, -5);  // Returns -5

This example constrains a value to the specified range:

function constrainToRange (checkVal, minVal, maxVal) {
  return Math.min(Math.max(checkVal, minVal), maxVal);
}
// Constrain the slider to the stage area
mySlider._x = constainToRange (mySlider._x, 0, 550);

This example returns the maximum value in an array:

function maxInArray (checkArray) {
  maxVal = -Number.MAX_VALUE;  // Initialize maxVal to a very small number
  for (var i = 0; i < checkArray.length; i++) {
    maxVal = Math.max(checkArray[i], maxVal);
  }
  return maxVal;
}

trace(maxInArray([2,3,66,4,342,-90,0]));  // Displays: 342

See Also

Math.min( )

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.