- Check
22 ≥ 14and22 ≥ 7- Verdict
- Matches the sample script output.
Find Biggest of Three Numbers in JavaScript
What you’ll learn
- How to pick the largest of three values with a clear if–else if–else ladder.
- An equivalent nested
max2style (same result, different shape), plus howMath.maxrelates. - Why
>=matters when values can tie, plus a browser live preview.
Overview
Given three numbers, return the one that is not smaller than the other two. When two or three values tie for largest, any of the tied values is a correct “biggest”; the scripts on this page return one of them deterministically.
Two scripts
Classic if-ladder (interview style) plus a max2 composition.
Live preview
Type three numbers and see the maximum immediately (same ordering as Math.max).
Interview polish
Mention ties, extension to N values, and O(1) cost for three fixed slots.
Prerequisites
Comfort with if, else if, else, and relational operators >, >=, plus &&.
const/let,console.log, and numeric literals.- Optional: average of N numbers for nearby numeric interview practice.
What does “biggest of three” mean?
For values a, b, and c, the maximum (biggest) is any value m in {a,b,c} such that m ≥ a, m ≥ b, and m ≥ c. If the largest value appears more than once, those inequalities still hold.
The reference-style program uses >= on both sides so ties are classified into the first matching branch without falling through incorrectly.
Formal note
Binary max is associative in the usual sense for totally ordered types: max(a, b, c) = max(max(a, b), c). That identity is the correctness argument behind the two-argument helper version.
max(14, 7, 22) = 22 because 22 is greater than or equal to both 14 and 7.
Intuition and examples
Picture three poles of different heights: scan with your eyes from left to right, remembering the tallest seen so far. For exactly three fixed slots, you can hard-code the same comparisons.
- Tie
- Two values share the maximum.
- Verdict
>=branches return 5 (first branch wins here).
- Triple tie
- Every value is largest.
- Verdict
- First branch returns 9.
Takeaway: the problem is total order comparison, not sorting the whole list.
Live preview
Enter three numbers. The widget uses Math.max on three arguments (handles ties the same way as the ECMAScript definition).
Algorithm
Goal: return the largest of three comparable values a, b, c.
Test whether a wins
If a ≥ b and a ≥ c, then a is a maximum; return it.
Else test whether b wins
If b ≥ a and b ≥ c, return b.
Otherwise return c
If neither a nor b dominates both others, c must.
Alternative: fold with max2 or Math.max
Let max2(u,v) return the larger of two values. Then max3(a,b,c) = max2(max2(a,b), c). Equivalently, Math.max(a, b, c) in JavaScript.
📜 Pseudocode
function findBiggest(a, b, c):
if a >= b and a >= c:
return a
if b >= a and b >= c:
return b
return cIf–else if ladder (reference style)
Matches the classic interview solution: three mutually exclusive branches with && and >=.
function findBiggest(num1, num2, num3) {
if (num1 >= num2 && num1 >= num3) {
return num1;
} else if (num2 >= num1 && num2 >= num3) {
return num2;
}
return num3;
}
const number1 = 14;
const number2 = 7;
const number3 = 22;
const result = findBiggest(number1, number2, number3);
console.log("The biggest number is:", result);Explanation
Each branch names one candidate and proves it beats (or ties) the other two.
num1 >= num2 && num1 >= num3Candidate num1. Both comparisons must hold so num1 is not smaller than either rival.
else if (num2 >= num1 && num2 >= num3)Only if the first test failed. Then ask the same style of question for num2.
return num3;Last case. No third if is needed: if num1 and num2 are not maximal, num3 is.
Nested two-argument maximum
Same semantics, compact form. Easy to generalize mentally to “fold” over longer arrays in a loop.
function max2(a, b) {
return a > b ? a : b;
}
function findBiggest3(a, b, c) {
return max2(max2(a, b), c);
}
const number1 = 14;
const number2 = 7;
const number3 = 22;
const result = findBiggest3(number1, number2, number3);
console.log("The biggest number is:", result);Explanation
max2 uses strict >; ties pick the left operand, which is fine because any tied largest is acceptable.
max2(max2(a, b), c)Associative fold. First reduce the first pair, then compare the winner against c.
Optimization
Three values only. Any correct solution is already O(1); micro-optimizations are rarely asked.
Larger N. Read into an array and track best in one pass, or use Math.max(...values) for moderate lists (mind call-stack/arg limits for huge spreads).
Built-in. Math.max(a, b, c) is idiomatic when you do not need to show every comparison.
Interview: write the if-ladder first for clarity; offer max2 nesting or Math.max as a follow-up.
❓ FAQ
🔄 Input / output examples
Both sample scripts print one line. Swap the const literals or read from process.argv / prompts for interactive runs.
Inputs (a, b, c) | Printed line |
|---|---|
14, 7, 22 | The biggest number is: 22 |
5, 5, 3 | The biggest number is: 5 |
-1, -4, -2 | The biggest number is: -1 |
Edge cases and pitfalls
Watch branch order when you use only strict >, and be explicit about ties if the problem wants a specific tie-break rule.
Two or three equal maxima
The if-ladder with >= returns the first candidate that can tie-win. That is usually acceptable; if the spec needs the smallest index among ties, say so and adjust.
All values below zero
Comparisons still work: the “biggest” is the least negative (closest to zero).
Non-numeric data
If any operand is NaN, Math.max returns NaN; comparisons with NaN are false, so guard inputs when building interview-grade APIs.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
If-ladder or nested max2 for three numbers | O(1) | O(1) |
Max of N numbers (single pass) | O(N) | O(1) |
Both scripts on this page use only a few locals: auxiliary space is constant.
Summary
- Task: return the maximum of three numbers (ties allowed).
- Patterns: if-ladder with
>=and&&, ormax2(max2(a,b),c), orMath.max(a,b,c). - Watch-outs: tie semantics with strict
>only,NaNinputs, and scaling toNwith a loop.
Comparing three values takes at most three pairwise checks in the obvious if-ladder form. The same answer can be built by nesting max(a,b) twice—or by calling Math.max(a,b,c) when you want the built-in. All are O(1) time and O(1) space.
9 people found this page helpful
