Home >>Javascript Programs >Find the larger of two numbers in JavaScript

Find the larger of two numbers in JavaScript

How to Find the larger of two numbers in JavaScript

In this article, we will learn how to find the larger of two numbers in JavaScript. Here we have two number one is smaller and of-course another number will be greater. We will create a function to return the larger of two numbers when the user enters a number in the input. So, let's begin to write the code of JavaScript to find the larger of two numbers.

Let's take an example:

<!doctype html>
<html>
<head>
<script>
			function largesstnum()
			{
				var num1, num2, num3;
				num1 = Number(document.getElementById("fnum").value);
				num2 = Number(document.getElementById("snum").value);

				if(num1>num2)
				{
					window.alert(num1+"-is Larger");
				}
				else if(num2>num1)
				{
					window.alert(num2+"-is Larger");
				}
				else if(num2>num1 && num2>num1)
				{
					window.alert(num2+"is Larger");
				}
			}
		</script>
	</head>
	<body>
		<h1>Check largest number </h1>
		<br>
		Enter number 1: <input type="text" id="fnum"></input><br>
		Enter number 2: <input type="text" id="snum"></input><br>
<button onclick="largesstnum()">submit</button>
	</body>
</html>
Output:

Check largest number


Enter number 1:
Enter number 2:


No Sidebar ads