CoffeeScript Cookbook » Max Array Value | Latest TMZ Celebrity News & Gossip | Watch TMZ Live

óCoffeeScript Cookbook

Max Array Value

Problem

You need to find the largest value contained in an array.

Solution

You can use Math.max() JavaScript method along with splats.

Math.max [12, 32, 11, 67, 1, 3]...
# => 67

Alternatively, it’s possible to use ES5 reduce method. For backward compatibility with older JavaScript implementations, use the above.

# ECMAScript 5
[12,32,11,67,1,3].reduce (a,b) -> Math.max a, b
# => 67

Discussion

Math.max compares every argument and returns the largest number from arguments. The ellipsis (...) converts every array value into argument which is given to the function. You can also use it with other functions which take variable amount of arguments, such as console.log.

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.