How to find the largest of three numbers in JavaScript
In this Answer, we will discuss ways to find the largest of three numbers in JavaScript.
There are several methods that can be used to solve this problem, like using:
The if-else statement
The switch case statement
The math object
Math.max()
Here, the if-else and switch case statements are conditional statements. These statements allow for different actions to be carried out based on various conditions.
The if-else statement
We can use the if-else statements to find the largest of three numbers. Let's see the code below.
Code example 1
// declaring the variableslet num1 = 30;let num2 = 70;let num3 = 15;let largestNum;if (num1 > num2 && num1 > num3) {largestNum = num1;}else if (num2 > num1 && num2 > num3) {largestNum = num2;}else {largestNum = num3;}console.log(largestNum);
Code explanation 1
Here is the explanation to the presented code.
Lines 2–4: Here,
num1,num2, andnum3are declared, which represent the three numbers.Line 5: The
largestNumis initialized without any value.Lines 7–9: We check if
num1is greater thannum2andnum3. If this is true, thenlargestNumis assigned the value ofnum1.Lines 10–12: We check if the first condition is not true; the code checks if
num2is greater than bothnum1andnum3. If this is true, thenlargestNumis assigned the value ofnum2.Lines 13–15: We check if neither of the first two conditions are true. In this case,
largestNumis assigned the value ofnum3.Line 17: We print out the largest number.
The switch case statement
The switch case statements can also be used to solve this problem. Let's see the code below.
Code example 2
// declaring of variableslet num1 = 70;let num2 = 40;let num3 = 15;let largestNum;switch (true) {case num1 > num2 && num1 > num3:largestNum = num1;break;case num2 > num1 && num2 > num3:largestNum = num2;break;default:largestNum = num3;}console.log(largestNum);
Code explanation 2
Here is the explanation to the presented code.
Line 8: We use a
switchstatement with the expression (true) as the argument to determine which of the three variables,num1,num2, ornum3, is the largest and assign that value to thelargestNumvariable.Lines 9–10: The variables are compared using conditional statements (the
>operator) to determine which of them is the largest. Ifnum1is the largest, its value is assigned tolargestNum.Lines 12–14: We check if
num2is the largest. If so, its value is assigned tolargestNum.Lines 15–16: If neither
num1nornum2is the largest, thennum3is the largest and its value is assigned tolargestNum.
The Math.max() method
The Math object allows us to perform mathematical tasks on numbers. The Math.max() method is a commonly used Math method that returns the highest value of a list.
Let's try this in the example below.
Code example 3
let num1 = 20;let num2 = 70;let num3 = 15;let largestNum = Math.max(num1, num2, num3);console.log("Three numbers are", num1, num2, "and", num3);console.log("Largest of these numbers is", largestNum);
Code explanation 3
Here is the explanation to the presented code.
Lines 1–3: Here,
num1,num2, andnum3are declared, and they represent the three numbers.Line 5: The
Math.max()method takes these three numbers as arguments and returns the largest number among them, which is assigned to thelargestNumvariable.Lines 7–8: We print all three numbers and then the value of the
largestNumvariable.