The native method of JavaScript gets the maximum (minimum) value in an array _ basics

Source: Internet
Author: User
The simplest way to get the maximum (minimum) value in an array is to iterate through the arrays and find the maximum (minimum) value by comparison. But in fact, in the native method of JavaScript, there are some quick ways to implement this functionality.
1 Array.prototype.sort
Copy Code code as follows:

var a = [7,3,4,6,10];
A.sort (function (a,b) {
return (a-b);})

Note that the comparison function in sort must be passed in, and if this function is not passed, the result of A.sort () is [10,3,4,6,7];

2 Math.max,math.min
Copy Code code as follows:

var a = [7,3,4,6,10];
var max = Math.max.apply (math,a);
var min = Math.min.apply (math,a);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.