This tutorial will explain how to find the largest number among the three numbers provided in JavaScript. We can either use the Conditional Operators and if…else statement or the in-built Math.max()
function of JavaScript.
Example 1: Using Conditional Operators and if…else statement.
// program to find the largest among three numbers
function largestNumber(x, y, z) {
max = 0;
if (x > y)
{
max = x;
} else
{
max = y;
}
if (z > max)
{
max = z;
}
return max;
}
console.log(largestNumber(10,100,1000));
console.log(largestNumber(55,6,78));
console.log(largestNumber(1,-22,-33));
Output
1000
78
1
Example 2: Using Math.max()
Syntax
Math.max(x, y, z);
// program to find the largest among three numbers
console.log(Math.max(10,100,1000));
console.log(Math.max(55,6,78));
console.log(Math.max(1,-22,-33));
Output
1000
78
1