Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript gets the maximum and minimum values from an array

Shulou Source: shulou.com Published: 2022-06-01 03:49:12 10月19日 Update

Editor to share with you how JavaScript from the array to get the maximum and minimum values, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Get the maximum and minimum values from the array

You can use Math.max and Math.min to fetch the maximum and minimum values in an array:

Const numbers = [15, 80,-9, 90,-99] const maxInNumbers = Math.max.apply (Math, numbers) const minInNumbers = Math.min.apply (Math, numbers) console.log (maxInNumbers) > Result: 90console.log (minInNumbers) > Result:-99

You can also use ES6's. Operator to complete:

Const numbers = [1, 2, 3, 4]; Math.max (... numbers) > Result: 4Math.min (... numbers) > > Result: 1 is all the content of the article "how to get the maximum and minimum values from an array by JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Maximum minimum array maximum article content not much most more knowledge industry information information channel operator channel reference learning help operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno MariaDB Shulou Technology Microsoft