Calculate min/max among set of numbers in JavaScript


I wanted to check the min/max among set of numbers in JavaScript but unfortunately there is no direct method available in JavaScript that can provide the min/max values among set of elements. But wait in JavaScript there is an method called ‘apply’, this is part of the parent Object which every thing is derived in JavaScript. In this article we would learn on how we can compute the min/max among set of elements using JavaScript without using any loops.

Function.prototype.apply:
Function.prototype.apply allow use to call the function with the given this value and argument which takes an array of elements. This function is available with all the Objects used in JavaScript.

Syntax
Function.apply(thisArg[, argsArray])

Now lets jump into the code required to compute min/max among set of numbers in JavaScript.
Assume we have set of number in an array

Now i want to get the min among the above array, the traditional way is to loop through and get the minimum element but using apply function we have to write only one line of code.

Tagged . Bookmark the permalink.

One Response to Calculate min/max among set of numbers in JavaScript

Leave a Reply