James
James

Reputation: 43647

jQuery get biggest number from list

var one = 1415;
var two = 2343;
var three = 11;

How to get the biggest number from these variables?

Upvotes: 9

Views: 17122

Answers (7)

sreejith unni
sreejith unni

Reputation: 1

 var number=[12,34,54,32,11,2]

 var big= Math.max.apply(null,number);

Upvotes: -1

Shivam Srivastava
Shivam Srivastava

Reputation: 4596

That will work 100%

var max = Math.max.apply(Math, "your array");

Upvotes: 1

Kevin
Kevin

Reputation: 11

If your values are in an array, try reduce :

var biggestValue = myArray.reduce( function(a,b){ return a > b ? a : b ; } );

Upvotes: 1

Todd Yandell
Todd Yandell

Reputation: 14696

Put them in an array, sort them, and take the last of the sorted values:

[one, two, three].sort(function (a, b) {
  return a > b ? 1 : (a < b ? -1 : 0);
}).slice(-1);

Upvotes: 1

kennebec
kennebec

Reputation: 104780

function biggestNumber(){
    return Math.max.apply(this,arguments);
}

var one= 1415;
var two= 2343;
var three= 11;

biggestNumber(one, two, three)

/* returned value: (Number) 2343 */

Upvotes: 0

RightSaidFred
RightSaidFred

Reputation: 11327

If you have them in an array, you can do this:

var numbers_array = [1415, 2343, 11];

numbers_array.push( 432 ); // now the array is [1415, 2343, 11, 432]

var biggest = Math.max.apply( null, numbers_array );

Upvotes: 12

shabunc
shabunc

Reputation: 24741

Math.max(one, two, three)

Upvotes: 22

Related Questions