46

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?

Ben Shelock
  • 20,154
  • 26
  • 92
  • 125

6 Answers6

118

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
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 2
    +1 I don't know why this hasn't been picked up on before and it's by far the simplest solution. If you want it to work with an array just use Math.max.apply(Math, array) – John Foster Sep 13 '09 at 19:44
  • 7
    Google seems to love SO. I was googling 'javascript max' 45 minutes after the question was first posted, and found this answer on the first page :-) – Eric J. Sep 13 '09 at 20:05
21

In almost any language, one can use max twice:

Math.max(num1, Math.max(num2, num3))

As @CMS points out, JavaScript specifically allows an arbitrary number of parameters to Math.max:

Math.max(num1, num2, num3);
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 7
    Math.max can accept any arbitrary number of arguments, see my answer below: http://stackoverflow.com/questions/1418569/javascript-max-function-for-3-numbers/1418646#1418646 – Christian C. Salvadó Sep 13 '09 at 19:36
  • @CMS: +1 Very cool, I learned something. I'm not aware of another mainstream language that does that, though in principal any language that can accept a variable number of function arguments could. – Eric J. Sep 13 '09 at 20:01
  • 3
    17 people liked the answer but 2 downvoted. Unfortunately neither left a reason. Would be nice to understand the downvotes so that we all can learn... – Eric J. Sep 02 '11 at 23:25
2

Push your values into an array arr and use Math.min.apply(null, arr) or Math.max.apply(null, arr) for maximum and minimum values respectively:

var arr = [];
arr.push(value1);
arr.push(value2);
arr.push(value3);

var minValue = Math.min.apply(null, arr);
var maxValue = Math.max.apply(null, arr);
Scott Pelak
  • 406
  • 1
  • 9
  • 15
2

Using with the new spread operator

var num = [23,34,56,72,1,22]; 
Math.max(...num)

for more info

Ashish Yadav
  • 3,039
  • 1
  • 16
  • 23
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 12 '17 at 22:36
  • @Badacadabra The value of this answer you get to write a bit less code and get to use "spread operator" Now u should upvote I hope – Ashish Yadav Sep 15 '17 at 13:36
0

let array = [1,2,3,4,5,6];

let array = [1,2,3,4,5,6];
let largest = 0;
for(let i = 0; i<=largest; i++){
(largest<array[i]? largest = array[i]:0)
}
console.log(largest);
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Sep 03 '21 at 06:55
-1
 var numbers = [2,3,1];
 numbers.sort(function(a,b){return b-a});
 console.log("max number is", numbers[0]); // max number is 3
Mani
  • 17,549
  • 13
  • 79
  • 100
atom
  • 692
  • 7
  • 11