How to compare two numbers in JavaScript?


In this tutorial, we will learn to compare two numbers in JavaScript. If you are a competitive coder or application developer, in these cases, you need to make a comparison between two numbers and need to perform the particular operation basis on the comparison result of the numbers.

Still, many programmers think we can compare the number only using the equality operator. Still, we can also compare the numbers using the less than or greater than operators.

We will see the use of all three operators to compare the number in this tutorial.

Using the Strict Equality Operator (===)

In JavaScript, there are two kinds of equality operators. One is simple equality, and another is a strict equality operator. The strict equality operator matches the values of the two variables, and the strict equality operator matches the values and data type of the variable.

Here, we need to make a comparison such that both variables should be the type of number and have equal values. So, we will use the strict equality operator.

Syntax

var number1 = 10;
var number2 = 20;
let result = number1 === number2;

Example

In the example below, we have compared two numbers using the strict equality operator. We are showing the message on the screen according to whether the number matches or not.

<html> <head> <title>Matching two numbers in JavaScript.</title> </head> <body> <h2>Matching two numbers using <i> strict equality operator </i>.</h2> <h4>Comparison between 10 and 20.</h4> <div id = "numberOuput1"> </div> <h4>Comparison between 100 and 100.</h4> <div id = "numberOuput2"></div> <script> let numberOuput1 = document.getElementById("numberOuput1"); let numberOuput2 = document.getElementById("numberOuput2"); let number1 = 10; let number2 = 20; let isEqual = number1 === number2; numberOuput1.innerHTML = isEqual; isEqual = 100 === 100; numberOuput2.innerHTML = isEqual; </script> </body> </html>

Compare numbers using less than or greater than operators

The less than ( < ) and greater than ( > ) operators are useful to compare two numbers and check which number is smaller and which number is greater. However, we can also use the equality operators with less than and greater than operators which are called less than or equal to ( <= ) and greater than or equal to ( >= ), respectively.

Users can follow the below syntax to compare the two numbers using the less than or equal to and greater than or equal to operators.

Syntax

var number1 = 0;
var number2 = 5;
let result = number1 <= number2; // less than or equal to
let result = number1 >= number2; // greater than or equal to

Example

In the example below, we have used the <= and >= operator to compare the two numbers. Users can see the result of comparing different numbers in the output.

<html> <head> <title>Matching two numbers in JavaScript.</title> </head> <body> <h2>Comparing two numbers in JavaScript using the <i> less than or equal to and greater than or equal to operators. </i></h2> <h4>Comparison between 0 and 5.</h4> <div id = "numberOuput1"> </div> <h4>Comparison between 20 and 5.</h4> <div id = "numberOuput2"></div> <script> let numberOuput1 = document.getElementById("numberOuput1"); let numberOuput2 = document.getElementById("numberOuput2"); let number1 = 0; let number2 = 5; let isEqual = number1 <= number2; numberOuput1.innerHTML = isEqual; isEqual = 20 >= 5; numberOuput2.innerHTML = isEqual; </script> </body> </html>

Find the maximum between 3 numbers using comparison

In this section, we will find the maximum numbers between 3 numbers. To solve the problem, we can either use the less than or greater than the operator.

Users can follow the below syntax to find the maximum between three numbers using the ‘greater than’ operator.

Syntax

if (number1 > number2 && number1 > number3) {

   // if number1 is greater than other two numbers, number1 is the
   maximum
   maximum = number1;
} else if (number2 > number1 && number2 > number3) {
   maximum = number2;
} else {
   maximum = number3;
}

Example

In the below example, we have used the greater than operator to find the maximum between three numbers. We have used the if-else statement to check if any single number is greater than the other two numbers. However, if the user passes the same numbers, the below code will not work. It is only to compare the different numbers.

<html> <head> <title>Matching two numbers in JavaScript.</title> </head> <body> <h2>Find maximum between three numbers using the <i> greater than operator </i>.</h2> <h4>Finding the maximum between 10, 43, and 5.</h4> <div id = "result"></div> <script> let result = document.getElementById("result"); let number1 = 10; let number2 = 43; let number3 = 5; if (number1 > number2 && number1 > number3) { result.innerHTML = " maximum is " + number1; } else if (number2 > number1 && number2 > number3) { result.innerHTML = " maximum is " + number2; } else { result.innerHTML = " maximum is " + number3; } </script> </body> </html>

We have learned to compare the two numbers using the equality operators. We have seen three different examples to compare numbers. The first example shows the strict comparison between two numbers. In the third example, we have used the greater than operator to find the maximum between three numbers. In the second example, we have also used the less than and greater than the operator.

Updated on: 08-Aug-2022

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements