Smallest or largest value form Array
by scriptwerx
HTML
<p>Smallest: <span id="smallest"></span></p>
<p>Largest: <span id="largest"></span></p>
<p>However; this code is very short but it's not optimal.</p>
<p>See <a href="http://jsperf.com/smallest-largest-from-array" target="_blank">this jsperf</a> for a quick performance example.
JavaScript
// Short way to get the smallest & largest values from Arrays
var testArr = [2, 1, 7, 9, 4];
function smallestValue(arr) {
return Math.min.apply(Math, arr);
}
function largestValue(arr) {
return Math.max.apply(Math, arr);
}
document.getElementById('smallest').innerHTML = smallestValue(testArr);
document.getElementById('largest').innerHTML = largestValue(testArr);