Finding the largest of three numbers is a classic way to practice comparison operators and nested conditions. JavaScript also ships a built-in shortcut, Math.max(), which does the same job in one line — useful to know once you understand how the manual version works.
Program to Find the Largest of Three Numbers Using if...else
function largestOfThree(a, b, c) {
if (a >= b && a >= c) {
return a;
} else if (b >= a && b >= c) {
return b;
} else {
return c;
}
}
console.log(largestOfThree(23, 89, 47));
Output:
89
Each branch uses >= (not just >) so that the function still returns a sensible value when two or more numbers are equal — for example, largestOfThree(5, 5, 3) correctly returns 5.
Program Using Math.max()
const a = 23, b = 89, c = 47;
const largest = Math.max(a, b, c);
console.log("Largest number:", largest);
Output:
Largest number: 89
Math.max() accepts any number of arguments, so the same function also works for four, five, or more numbers — or, combined with the spread operator, for an entire array: Math.max(...numbersArray).
