Getting the Maximum Number in a Set

Math.max(8,"0x5",12,"44",8,23,77); //returns 77 var arr1 = [4,8,12,3,7,11]; Math.max.apply( Math, arr1 ); //returns 12 var arr2 = ["0x5",8,12,"4",308,23,77,"0xFF"]; Math.max.apply( Math, arr2 ); //returns 308

The Math.max(item, item, ...) method allows you to find the largest number in a set. You can also apply an array object to the .max() method using Math.max.apply(Math, array). This returns the largest number in the array. The array can contain string representations of numbers such as "26" and "0x1F", but not character strings such as "A". If the array contains character strings, the result will be NaN.

Get jQuery and JavaScript Phrasebook 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.