JS Example for Beginners: JavaScript Program to Find the Largest Among Three Numbers

(JavaScript programming Example for Beginners)

JavaScript Program to Find the Largest Among Three Numbers

In this example, you will learn to find the largest among three numbers in JavaScript.


You can find the largest among three numbers using the if...else statement.

Example 1: Largest Number Among Three Numbers

// program to find the largest among three numbers

// take input from the user
let num1 = parseFloat(prompt("Enter first number: "));
let num2 = parseFloat(prompt("Enter second number: "));
let num3 = parseFloat(prompt("Enter third number: "));
let largest;

// check the condition
if(num1 >= num2 && num1 >= num3) {
    largest = num1;
}
else if (num2 >= num1 && num2 >= num3) {
    largest = num2;
}
else {
    largest = num3;
}

// display the result
console.log("The largest number is " + largest);

 

Output

Enter first number: -7
Enter second number: -5
Enter third number: -1
The largest number is -1

In the above program, parseFloat() is used to convert numeric string to number. If the string is a floating number, parseFloat() converts the string into floating point number.

The numbers are compared with one another using greater than or equal to >= operator. And the if...else if...else statement is used to check the condition.

Here, logical AND && is also used to check two conditions.


You can also use the JavaScript built-in Math.max() function to find the largest among the numbers.

Example2: Using Math.max()

// program to find the largest among three numbers

// take input from the user
let num1 = parseFloat(prompt("Enter first number: "));
let num2 = parseFloat(prompt("Enter second number: "));
let num3 = parseFloat(prompt("Enter third number: "));

let largest = Math.max(num1, num2, num3);

// display the result
console.log("The largest number is " + largest);

Output

Enter first number: 5
Enter second number: 5.5
Enter third number: 5.6
The largest number is 5.6

Math.max() returns the largest number among the provided numbers.


You can use Math.min() function to find the smallest among the numbers.

 

JS Example for Beginners: JavaScript Program to Find the Largest Among Three Numbers

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.