Write a JavaScript program to find the largest of three numbers

Description: Find the largest of the three numbers 25, 78 and 42.

Hint:

  • Compare with if and &&.
  • Or use Math.max().

JavaScript program for the largest of three numbers

const a = 25, b = 78, c = 42;

let largest;
if (a >= b && a >= c) {
  largest = a;
} else if (b >= a && b >= c) {
  largest = b;
} else {
  largest = c;
}

console.log("Largest:", largest);
console.log("Using Math.max:", Math.max(a, b, c));

Output:

Tutorials Class - Output Window

Largest: 78
Using Math.max: 78

How it works

  • The first if checks if a is at least as big as both others.
  • Math.max() does the same in one call.

Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.