Largest Among Three Numbers in JavaScript
Find the largest of three numbers in JavaScript with if-else, Math.max and the spread operator. Shows why >= handles ties, with explanation and output.
1// Method 1: Using if-else2let a = 10, b = 25, c = 15;3let largest;45if (a >= b && a >= c) {6 largest = a;7} else if (b >= a && b >= c) {8 largest = b;9} else {10 largest = c;11}1213console.log("Largest among", a + ",", b + ",", c, "is:", largest);1415// Method 2: Using Math.max()16let num1 = 10, num2 = 25, num3 = 15;17let max = Math.max(num1, num2, num3);18console.log("Largest using Math.max():", max);1920// Method 3: Using function21function findLargest(x, y, z) {22 if (x >= y && x >= z) {23 return x;24 } else if (y >= x && y >= z) {25 return y;26 } else {27 return z;28 }29}3031console.log("Largest:", findLargest(5, 8, 3));3233// Method 4: Using array and spread operator34function findLargestArray(...numbers) {35 return Math.max(...numbers);36}3738console.log("Largest from array:", findLargestArray(10, 25, 15, 30, 5));
Sample Output
Largest among 10, 25, 15 is: 25
a + "," joins the number and comma into one string like "10,", so there is no space before each comma.
In this one
- How to compare numbers with && in if-else
- Why >= is needed for ties
- How Math.max works
- How rest and spread syntax pass many values
How it works
- 1
a = 10, b = 25, c = 15. b passes its check, so largest is 25.
- 2
Math.max(10, 25, 15) also returns 25.
- 3
findLargest(5, 8, 3) returns 8.
- 4
findLargestArray collects 5 numbers and returns 30.
Edge cases
Ties with >
a = 25, b = 25, c = 10 with > picks c.
Neither strict check passes, so the else runs. Use >= to keep ties correct.
Strings instead of numbers
Math.max("9", "10") returns 10, but "9" > "10" is true.
Math.max converts to numbers, but > on two strings compares them as text. Convert input first.
No arguments
Math.max() returns -Infinity.
With nothing to compare, the answer is -Infinity. Check that you passed values.
What's going on
This program finds the biggest of three numbers. Imagine three students and their test marks. You want the top score. The program shows four ways to find it.
Method 1 uses if-else. It asks: is a at least as big as both b and c? If yes, a wins. If not, is b at least as big as a and c? If yes, b wins. Otherwise c must be the largest.
Use >= and not > in these checks. Ties matter. If a and b are both 25 and you use >, neither a > b nor b > a is true. The code then falls through to c, even though c might be smaller. With >= a tie still picks a correct maximum.
Method 2 uses Math.max(num1, num2, num3). It is a built-in function that returns the largest value you pass in. It is shorter and is what you would use in real code. It is still worth writing the comparisons once, so the logic lives in your head.
Method 3 wraps the if-else version in a function called findLargest. Method 4 uses rest and spread. The rest parameter ...numbers collects any number of arguments into an array. Then Math.max(...numbers) spreads that array back out into separate values.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does Math.max(4, 9, 2) return?
Pick an answer to check it.
Question 2
Why use >= instead of > here?
Pick an answer to check it.
Questions people ask
Which method should I use?
Math.max for real code. Write the if-else once to understand comparison logic.
How do I find the smallest number?
Use Math.min, or flip >= to <= in the if-else.
Can Math.max take an array directly?
No. Math.max([1, 2]) is NaN. Spread it: Math.max(...arr).