JavaScript Biggest of Three Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Biggest of Three Numbers?

Given three values a, b, and c, the biggest is the one that is greater than or equal to both of the others.

Remember
Example: a = 14, b = 7, c = 22
  22 >= 14 and 22 >= 7  → biggest is 22

Ties: a = 10, b = 10, c = 3
  either 10 is a valid maximum
abcBiggest
1472222
1010310
-5-2-9-2
5555

How to Find the Biggest of Three Numbers in JavaScript

Compare pairwise (or call Math.max) and prefer >= so ties stay correct.

  1. If a >= b and a >= c, return a.
  2. Else if b >= a and b >= c, return b.
  3. Otherwise return c.
  4. Shortcut: Math.max(a, b, c).
MethodIdeaTimeBest for
If-else ladderExplicit >= checksO(1)Interviews, default
Math.maxMath.max(a, b, c)O(1)Short production code
Nested Math.maxMath.max(a, Math.max(b, c))O(1)Teaching pairwise max

Pseudocode

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

Cheat Sheet

Keep these patterns handy while coding.

TopicDetail
a winsa >= b && a >= c
b winsb >= a && b >= c
elsereturn c
One-linerMath.max(a, b, c)
Many valuesUse a running-max loop (see Maximum of Array)

Live Preview

Enter three numbers to see which is biggest.

Integers and decimals both work. Tap a chip or type your own values.

Result
a = 14, b = 7, c = 22
biggest = 22

JavaScript Programs

Start with an if-else ladder, then use Math.max, and finish with nested pairwise max. Each example has a Try it Yourself editor.

Example 1 — If-Else Ladder

Explicit comparisons with >= — the interview default.

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

How It Works

  1. If num1 is at least as large as the other two, return it.
  2. Else if num2 wins both comparisons, return num2.
  3. Otherwise num3 must be the maximum.
  4. >= keeps ties correct (equal largest values still return a valid max).
  5. 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));
Try It Yourself

How It Works

  1. Math.max returns the largest of its arguments.
  2. Pass all three values in one call: Math.max(a, b, c).
  3. Same answer as the if-else ladder — fewer lines.
  4. Still explain the ladder in interviews before relying on the built-in.
  5. 14, 7, 22 → 22.

Example 3 — Nested Math.max

Pairwise nesting — useful when teaching two-argument max.

JavaScript
function findBiggestNested(a, b, c) {
  return Math.max(a, Math.max(b, c));
}

console.log(findBiggestNested(5, 5, 3));
console.log(findBiggestNested(-1, -4, -2));
Try It Yourself

How It Works

  1. Inner Math.max(b, c) picks the larger of the last pair.
  2. Outer Math.max(a, …) compares that result with a.
  3. Ties: 5 vs 5 vs 3 → 5.
  4. Negatives: −1 vs −4 vs −2 → −1.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
If-else / Math.max / nestedO(1)O(1)
Running max over N valuesO(N)O(1)

Three fixed inputs need only a few comparisons — constant time and space.

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.

Next: Binary to Decimal

Convert a binary string into its decimal integer value.

Binary to decimal tutorial →

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.

16 people found this page helpful