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.
We can use the if-else statements to find the largest of three numbers. Let's see the code below.
// 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);
Here is the explanation to the presented code.
Lines 2–4: Here, num1
, num2
, and num3
are declared, which represent the three numbers.
Line 5: The largestNum
is initialized without any value.
Lines 7–9: We check if num1
is greater than num2
and num3
. If this is true, then largestNum
is assigned the value of num1
.
Lines 10–12: We check if the first condition is not true; the code checks if num2
is greater than both num1
and num3
. If this is true, then largestNum
is assigned the value of num2
.
Lines 13–15: We check if neither of the first two conditions are true. In this case, largestNum
is assigned the value of num3
.
Line 17: We print out the largest number.
The switch case statements can also be used to solve this problem. Let's see the code below.
// 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);
Here is the explanation to the presented code.
Line 8: We use a switch
statement with the expression (true
) as the argument to determine which of the three variables, num1
, num2
, or num3
, is the largest and assign that value to the largestNum
variable.
Lines 9–10: The variables are compared using conditional statements (the >
operator) to determine which of them is the largest. If num1
is the largest, its value is assigned to largestNum
.
Lines 12–14: We check if num2
is the largest. If so, its value is assigned to largestNum
.
Lines 15–16: If neither num1
nor num2
is the largest, then num3
is the largest and its value is assigned to largestNum
.
Math.max()
methodThe 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.
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);
Here is the explanation to the presented code.
Lines 1–3: Here, num1
, num2
, and num3
are 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 the largestNum
variable.
Lines 7–8: We print all three numbers and then the value of the largestNum
variable.