JavaScript Code

How to compare two numbers in JavaScript?

Compare two numbers in JavaScript

In this tutorial, you are given two numbers. Write a JavaScript program to find a comparison between them: if first number is less than second number, first number is greater than second number, or if the two numbers are equal.


To compare two numbers, we can use comparison operators greater than, and less than.

We will use the above said operators in conditions of if-else-if statement to draw a comparison between the two given numbers.


In the following program, we are given two numbers are num1 and num2. We compare these two numbers and print the result.

let num1 = 4;
let num2 = 8;

if ( num1 > num2 ) {
    console.log(`${num1} is greater than ${num2}`);
} else if ( num1 < num2 ) {
    console.log(`${num1} is less than ${num2}`);
} else {
    console.log('given numbers are equal');