Javascript max()函数为3个数字 [英] Javascript max() function for 3 numbers
本文介绍了Javascript max()函数为3个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从3个不同的数字中找到最高的数字。我发现的唯一的东西是max(),但你只能使用2个数字。
I need to find the highest number from 3 different numbers. The only thing I've found is max() but you can only use 2 numbers.
最好的方法是什么?
推荐答案
数学.max 函数可以接受任意数量的参数:
The Math.max function can accept any arbitrary number of arguments:
语法:
Math.max([value1[,value2[, ...]]])
用法:
var max = Math.max(num1, num2, num3);
例如:
console.log(Math.max(1,2,3,4,5,6)); // 6
您甚至可以使用它来获取数字数组的最大值申请:
You could even use it to get the maximum value of an array of numbers with the help of apply:
function maxOfArray(array) {
return Math.max.apply(Math, array);
}
let array = [1,2,3,4,5,6];
console.log(maxOfArray(array)); // 6
如果您可以定位ES6(ES2015),那么也可以使用传播运营商:
If you can target ES6 (ES2015), you can also use the spread operator:
let array = [1,2,3,4,5,6];
let max = Math.max(...array);
console.log(max); // 6
这篇关于Javascript max()函数为3个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文