Find Biggest of Three Numbers in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Conditionals

What you’ll learn

  • How to pick the largest of three values with a clear if–else if–else ladder.
  • An equivalent nested max2 style (same result, different shape), plus how Math.max relates.
  • 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.

If-ladder explicit branches
Nested max max(a,b) then vs c
General N loop + running max

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.

Example

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.

14, 7, 22 Max 22
Check
22 ≥ 14 and 22 ≥ 7
Verdict
Matches the sample script output.
5, 5, 3 Max 5
Tie
Two values share the maximum.
Verdict
>= branches return 5 (first branch wins here).
9, 9, 9 Max 9
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).

Integers match the sample scripts; decimals are still valid Number inputs.

Live result
Press “Run” to see the biggest value.

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.

📜 Pseudocode

Pseudocode
function findBiggest(a, b, c):
    if a >= b and a >= c:
        return a
    if b >= a and b >= c:
        return b
    return c
1

If–else if ladder (reference style)

Matches the classic interview solution: three mutually exclusive branches with && and >=.

JavaScript
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);
Try it Yourself

Explanation

Each branch names one candidate and proves it beats (or ties) the other two.

num1 >= num2 && num1 >= num3

Candidate 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.

2

Nested two-argument maximum

Same semantics, compact form. Easy to generalize mentally to “fold” over longer arrays in a loop.

JavaScript
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);
Try it Yourself

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

Use an if-else if ladder with >= on both sides, nest a two-argument max helper twice, or call Math.max(a, b, c). All return a largest value; >= keeps behavior correct when two or all three numbers are equal.
If you only used strict >, two equal largest values could fall through incorrectly depending on branch order. Chaining >= against both other operands picks a winner whenever the current candidate is tied for largest.
It is clear and fast for three (or many) numbers. In interviews, still be able to write the explicit comparisons to show you understand the logic.
Use a loop with a running maximum, spread Math.max(...values) for moderate arrays, or reduce. Loops scale better for huge arrays than a giant Math.max argument list.
A fixed number of comparisons for three values is O(1) time and O(1) extra space.
Comparing numbers does not add them, so ordinary max-of-three does not overflow. Beware if you later compute sums or products as part of a larger problem.

🔄 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, 22The biggest number is: 22
5, 5, 3The biggest number is: 5
-1, -4, -2The 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.

Ties

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.

Negatives

All values below zero

Comparisons still work: the “biggest” is the least negative (closest to zero).

NaN

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

ApproachTimeExtra space
If-ladder or nested max2 for three numbersO(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 &&, or max2(max2(a,b),c), or Math.max(a,b,c).
  • Watch-outs: tie semantics with strict > only, NaN inputs, and scaling to N with a loop.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful