JavaScript: Find the Largest Among Three Numbers

1. Introduction

Comparing numbers to determine the largest among them is a foundational concept in mathematics and has significant utility in programming. Whether it's in data analysis, game development, or simple conditional operations, knowing how to efficiently compare numbers is essential. In this guide, we'll examine a JavaScript program that determines the largest among the three provided numbers.

2. Program Overview

Throughout this tutorial, we will:

1. Define three distinct numbers.

2. Write conditional statements to compare these numbers.

3. Identify and display the largest number.

3. Code Program

let num1 = 42;    // First number
let num2 = 78;    // Second number
let num3 = 55;    // Third number

let largest;      // To hold the largest number

// Using conditional statements to compare the numbers and determine the largest
if (num1 > num2 && num1 > num3) {
    largest = num1;
} else if (num2 > num1 && num2 > num3) {
    largest = num2;
} else {
    largest = num3;

console.log("The largest number among " + num1 + ", " + num2 + ", and " + num3 + " is: " + largest + ".");


The largest number among 42, 78, and 55 is: 78.

4. Step By Step Explanation

1. Variable Declaration: We initiate by assigning values to three variables (num1, num2, and num3). Additionally, we declare a variable named largest which will store the largest value after comparison.

2. Conditional Statements for Comparison: We use a series of if-else statements to compare the numbers. The conditions check each number against the other two to determine which is the largest.

3. Storing the Result: After the comparisons, the largest number is assigned to the largest variable.

4. Outputting the Result: The console.log function displays the largest number in a well-structured sentence, ensuring clarity in the result presentation.