Largest Among Three Numbers

Program to find the largest of three numbers

BeginnerTopic: Conditional Programs
Back

JavaScript Largest Among Three Numbers Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using if-else
let a = 10, b = 25, c = 15;
let largest;

if (a >= b && a >= c) {
    largest = a;
} else if (b >= a && b >= c) {
    largest = b;
} else {
    largest = c;
}

console.log("Largest among", a + ",", b + ",", c, "is:", largest);

// Method 2: Using Math.max()
let num1 = 10, num2 = 25, num3 = 15;
let max = Math.max(num1, num2, num3);
console.log("Largest using Math.max():", max);

// Method 3: Using function
function findLargest(x, y, z) {
    if (x >= y && x >= z) {
        return x;
    } else if (y >= x && y >= z) {
        return y;
    } else {
        return z;
    }
}

console.log("Largest:", findLargest(5, 8, 3));

// Method 4: Using array and spread operator
function findLargestArray(...numbers) {
    return Math.max(...numbers);
}

console.log("Largest from array:", findLargestArray(10, 25, 15, 30, 5));
Output
Largest among 10 , 25 , 15 is: 25
Largest using Math.max(): 25
Largest: 8
Largest from array: 30

Understanding Largest Among Three Numbers

This program demonstrates multiple approaches to find the largest number.

Method 1: If-Else Chain

Using logical AND (&&) operator:

if (a >= b && a >= c) {
} else if (b >= a && b >= c) {

    // b is largest
} else {

    // c is largest
}
    // a is largest

Logical Operators

&&: AND - both conditions must be true
||: OR - at least one condition must be true
!: NOT - reverses boolean value

Method 2: Math.max()

Built-in JavaScript function:

Math.max(a, b, c);

Pros:

Simple and concise
Works with any number of arguments
Built-in, optimized

Method 3: Function Approach

Reusable function with clear logic:

function findLargest(x, y, z) {
    if (x >= y && x >= z) return x;
    if (y >= x && y >= z) return y;
}
    return z;

Method 4: Spread Operator (ES6)

Using rest parameters:

function findLargestArray(...numbers) {
}
    return Math.max(...numbers);

Spread Operator (`...`)

Expands array/arguments
Math.max(...[1, 2, 3])Math.max(1, 2, 3)
Very flexible for multiple values

When to Use:

-

Math.max()

: Simplest, built-in

-

If-else

: Learning logic, custom conditions

-

Function

: Reusable code

-

Spread

: Variable number of inputs

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Largest Among Three Numbers

This JavaScript program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents