YogeshChauhan.com
How to Find the Highest (or Lowest) Number in an Array in JavaScript?
August 12, 2020

We saw the compare() and sort() function in details in this blog post:

The Sort And Compare Functions In JavaScript

We can use the same function to sort the numbers and then use the first and last numbers as min and max. It's a work around but it works.


var points = [40, 100, 1, 5, 25, 10];
points.sort(function (a, b) {
  return a - b;
});

console.log("lowest value = " + points[0]);
console.log("highest value = " + points[points.length - 1]);

// lowest value = 1
// highest value = 100

We can use the same trick with sorting the array in descending:


var points = [40, 100, 1, 5, 25, 10];
points.sort(function (a, b) {
  return b - a;
});

console.log(" highest value = " + points[0]);
console.log("lowest value = " + points[points.length - 1]);

// highest value = 100
// lowest value = 1
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

How to retrieve homepage URL in WordPress?WordPressValues and Types Basics in JavaScriptJavaScriptSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMisc5 Key Principles Of Good Website UsabilityUI/UXHow to Secure Web or Mobile Browsers?MiscHow to check if checkbox is checked or not using JavaScript?JavaScriptThe actual difference between indexOf() and search() in JavaScriptJavaScriptHow to convert an object from API to JSON array in Angular 9?AngularWhat is the best way to add JavaScript Code into HTML?HTMLMySQL queries to get data rows from previous month as well as from last 30 daysSQL/MySQLHow to create ‘share on LinkedIn’ link using just HTML?HTMLWhat is ECMAScript?JavaScript