The Wayback Machine - https://web.archive.org/web/20240307194813/https://www.geeksforgeeks.org/how-to-find-largest-of-three-numbers-using-javascript/
Open In App
Related Articles

How to find largest of three numbers using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

To find the largest of three numbers using JavaScript, we have multiple approaches. In this article, we are going to learn how to find the largest of three numbers using JavaScript.

Below are the approaches to finding the largest of three numbers using JavaScript:

Approach 1: Using Conditional Statements (if-else)

This is a straightforward approach using if-else statements to compare the numbers and find the largest one.

Example: In this example, we are using a Conditional Statement(if-else).

Javascript

function findLargest(num1, num2, num3) {
    if (num1 >= num2 && num1 >= num3) {
        return num1;
    } else if (num2 >= num1 && num2 >= num3) {
        return num2;
    } else {
        return num3;
    }
}
 
// Example usage
const largestNumber = findLargest(10, 5, 8);
console.log("Largest number:", largestNumber);

                    

Output
Largest number: 10

Approach 2: Using the Math.max() Method

The Math.max() method can be used to find the maximum of a list of numbers.

Example: In this example, we are using Math.max() Method.

Javascript

function findLargest(num1, num2, num3) {
  return Math.max(num1, num2, num3);
}
 
// Example usage:
const largestNumber = findLargest(10, 5, 8);
console.log("Largest number:", largestNumber);

                    

Output
Largest number: 10

Approach 3: Using the Spread Operator with Math.max()

Spread the numbers in an array using the spread operator and then use Math.max().

Example: In this example, we are using Spread Operator with Math.max().

Javascript

function findLargest(num1, num2, num3) {
  return Math.max(...[num1, num2, num3]);
}
 
// Example usage:
const largestNumber = findLargest(10, 5, 8);
console.log("Largest number:", largestNumber);

                    

Output
Largest number: 10

Approach 4: Using the Ternary Operator

The ternary operator can be used to concisely express the comparison.

Example: In this example, we are using Ternary Operator.

Javascript

function findLargest(num1, num2, num3) {
  return num1 >= num2 && num1 >= num3 ? num1
    : num2 >= num1 && num2 >= num3 ? num2
    : num3;
}
 
// Example usage:
const largestNumber = findLargest(10, 5, 8);
console.log("Largest number:", largestNumber);

                    

Output
Largest number: 10

Approach 5: Using Array.sort()

Put the numbers in an array and use Array.sort() to sort them in ascending order. The largest number will be at the end of the array.

Example: In this example, we are using Array.sort().

Javascript

function findLargest(num1, num2, num3) {
  const numbers = [num1, num2, num3];
  numbers.sort((a, b) => a - b);
  return numbers[numbers.length - 1];
}
 
// Example usage:
const largestNumber = findLargest(10, 5, 8);
console.log("Largest number:", largestNumber);

                    

Output
Largest number: 10


Last Updated : 03 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials