If num1 is at least as large as the other two, return it.
Else if num2 wins both comparisons, return num2.
Otherwise num3 must be the maximum.
>= keeps ties correct (equal largest values still return a valid max).
Sample 14, 7, 22 prints 22.
Example 2 — Math.max
Short and idiomatic — JS Math.max accepts any number of arguments.
JavaScript
function findBiggestWithMax(a, b, c) {
return Math.max(a, b, c);
}
const a = 14;
const b = 7;
const c = 22;
console.log("The biggest number is: " + findBiggestWithMax(a, b, c));
Inner Math.max(b, c) picks the larger of the last pair.
Outer Math.max(a, …) compares that result with a.
Ties: 5 vs 5 vs 3 → 5.
Negatives: −1 vs −4 vs −2 → −1.
Equivalent to Math.max(a, b, c) for three values.
Edge Cases & Pitfalls
Check these before calling the solution done.
Ties
Prefer >= over >
Equal largest values still return a valid maximum with >=.
Negatives
Largest can still be negative
Among −5, −2, −9 the biggest is −2 — do not assume a positive answer.
N values
Do not nest forever
For many numbers, use a running-max loop instead of nested ifs.
NaN
Invalid numbers
Math.max with NaN returns NaN. Validate inputs when needed.
Analysis
Time and Space Complexity
Program
Time
Extra space
If-else / Math.max / nested
O(1)
O(1)
Running max over N values
O(N)
O(1)
Three fixed inputs need only a few comparisons — constant time and space.
Remember
Key Takeaways
Rule: return the value that is >= both others.
Ties: use >= so equal maxima still work.
Shortcut:Math.max(a, b, c).
Test: 14, 7, 22 → 22; −5, −2, −9 → −2.
One line: compare three values; return the one that is not smaller than the other two.
Frequently Asked Questions
Compare with if-else using >=, or call Math.max(a, b, c). Both return the largest value.
>= handles ties. If two values share the maximum, one valid answer is still returned.
Yes. In JavaScript, Math.max accepts any number of arguments, so Math.max(a, b, c) works directly. In interviews, write the if-else version first to show the logic.
For exactly three numbers, time is O(1) and extra space is O(1).
Yes. Comparisons treat negatives the same way — among −5, −2, −9 the biggest is −2.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.