How to find largest of three numbers using JavaScript ?
Last Updated :
03 Jan, 2024
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:
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);
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);
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);
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);
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);
Similar Reads
How to create a Number object using JavaScript ? In this article, we will discuss how to create a Number object using JavaScript. A number object is used to represent integers, decimal or float point numbers, and many more. The primitive wrapper object Number is used to represent and handle numbers. examples: 20, 0.25. We generally don't need to w
2 min read
JavaScript Program to Find LCM of Two Numbers In this article, we are going to learn about finding the LCM of two numbers by using JavaScript. LCM (Least Common Multiple) of two numbers is the smallest positive integer that is divisible by both numbers without leaving a remainder. It's a common multiple of the numbers. LCM of two numbers = Prod
4 min read
How to Get the Longest String in an Array using JavaScript? Given an array, the task is to get the longest string from the array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. In this article, we will use JavaScript methods to find out the longest string in the array. All approaches are described below with e
5 min read
How to calculate multiplication and division of two numbers using JavaScript ? We will see the calculation such as multiplication and division using Javascript. An arithmetic operation operates on two numbers and numbers are called operands. Approach: Multiplication of two numbersCreate the HTML form to take input from the user to perform multiplication operations. Add javasc
2 min read
Finding the Largest of Three Numbers in R Determining the largest of a set of numbers is a common task in programming. This article will guide you through various methods to find the largest of three numbers in R, including using built-in functions, conditional statements, and user-defined functions. Here are the 4 main strategies for Findi
3 min read
Maximum of Two Numbers in JavaScript Finding the maximum of two numbers is a popular JavaScript operation used in many different programming tasks. This can be accomplished in a variety of ways, each having unique benefits and applications. There are several approaches for determining the maximum of two numbers in JavaScript which are
2 min read