How to useConditional Statements (if-else) in 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); |
Output
Largest number: 10
How to find largest of three numbers using JavaScript ?
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:
Table of Content
- Using Conditional Statements (if-else)
- Using the Math.max() Method
- Using the Spread Operator with Math.max()
- Using the Ternary Operator
- Using Array.sort()
Contact Us