Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Find Min or Max with sort()

Once you’ve sorted an array, you can leverage the index to retrieve the highest and lowest values.

Sort Ascending:

Example

const points = [40100152510];
points.sort(function(a, b){return a – b});
// now points[0] contains the lowest value
// and points[points.length-1] contains the highest value

Sort Descending:

Example

const points = [40100152510];
points.sort(function(a, b){return b – a});
// now points[0] contains the highest value
// and points[points.length-1] contains the lowest value
NOTE: Sorting the entire array is an inefficient approach if your objective is solely to determine the highest or lowest value.