Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  97] [ 4]  / answers: 1 / hits: 58936  / 15 Years ago, sun, september 13, 2009, 12:00:00

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.



Whats the best way?


More From » math

 Answers
5

The Math.max function can accept any arbitrary number of arguments:



Syntax:



Math.max([value1[,value2[, ...]]]) 


Usage:



var max = Math.max(num1, num2, num3);


For example:



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





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




[#98706] Tuesday, September 8, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;