How to find min max of an Array in JavaScript

This short code will help you easily find max min element in an array in JavaScript.

Create min max array prototype

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};
var arr = [10, 20, 5, 30, 9];

console.log(arr.min()) 
// 5 

console.log(arr.max()) 
// 30

Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply’ing Math.xxx() to your array directly:

var min = Math.min.apply(null, arr),
    max = Math.max.apply(null, arr);

Alternately, assuming your browser supports ECMAScript 6, you can use the spread operator which functions similarly to the apply method:

var min = Math.min( ...arr ),
    max = Math.max( ...arr );

#javascript #min #max

14.50 GEEK