Comparison operator in JavaScript

What you will learn here about javascript

  • comparison operator in javascript

Comparison operator in javascript allows user to compare two datas or values with each other. JavaScript supports following comparison operator

  1. greter than ( > )
  2. less than ( < )
  3. greter than equal to ( >= )
  4. less than equal to ( <= )
  5. equal to ( = = )
  6. equal to both type as well as value( = = = )
  7. not equal to ( != )
  8. not equal to both type as well as value( != = )

1)greter than ( > )

Greater than operator compares two values and return true if value1 is greater than value 2 otherwise returns false.

var number1=100;
var number2=50;
if(number1>number2)
{
document.getElementById(‘result’).innerHTML=”Number 1 is greater than number 2″;
}
else
{
document.getElementById(‘result’).innerHTML=”Number 2 is greater than number 1″;
}

2)less than ( < )

less than operator compares two values and return true if value1 is less than value 2 otherwise returns false.

var number1=100;
var number2=50;
if(number1<number2)
{
document.getElementById(‘result’).innerHTML=”Number 1 is less than number 2″;
}
else
{
document.getElementById(‘result’).innerHTML=”Number 2 is less than number 1″;
}

3)greter than equal to ( >= )

Greater than equal to operator compares two values and return true if value1 is greater than value 2 and when both values are equal otherwise returns false.

var number1=100;
var number2=50;
if(number1>=number2)
{
document.getElementById(‘result’).innerHTML=”Number 1 is greater than number 2″;
}
else
{
document.getElementById(‘result’).innerHTML=”Number 2 is greater than number 1″;
}

4)less than equal to ( <= )

less than equal to operator compares two values and return true if value1 is less than value 2 and when both values are equal otherwise returns false.

var number1=100;
var number2=50;
if(number1<=number2)
{
document.getElementById(‘result’).innerHTML=”Number 1 is less than number 2″;
}
else
{
document.getElementById(‘result’).innerHTML=”Number 2 is less than number 1″;
}

5)equal to ( = = )

Equal to operator compares two values and returns true only if two values are equal otherwise it returns false. But it compare only values not the data type.

var number1=100;
var number2=100;
if(number1= =number2)
{
document.getElementById(‘result’).innerHTML=”Both numbers are equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are not equal”;
}


equal to operator returns true even data type are different but values are same.

var number1=100; //this is number
var number2=”100″; //this is String
if(number1= =number2)
{
document.getElementById(‘result’).innerHTML=”numbers are equals even data type is different”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are not equal”;
}

6)equal to both type as well as value( = = = )

Equal to operator compares two values as well as data type and returns true only if two values are equal as well as data type otherwise it returns false.

var number1=100; //this is number
var number2=100; //this is number
if(number1= = =number2)
{
document.getElementById(‘result’).innerHTML=”numbers are equals as well as data type is also equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are not equal”;
}


= = = operator returns true only when both data type as well as values are same otherwise returns false.

var number1=100; //this is number
var number2=”100″; //this is String
if(number1= =number2)
{
document.getElementById(‘result’).innerHTML=”numbers are equals even data type is different”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are not equal”;
}

7)not equal to ( != )

not equal to operator compares two values and returns true if values are not equal otherwise returns false. not equal to only compares values not the data type.

var number1=100; //this is number
var number2=10; //this is number
if(number1!=number2)
{
document.getElementById(‘result’).innerHTML=”numbers are not equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are equal”;
}


equal to operation when data type is not same.

var number1=100; //this is number
var number2=”10″; //this is String
if(number1!=number2)
{
document.getElementById(‘result’).innerHTML=”numbers are not equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are equal”;
}

8)not equal to both type as well as value( != = )

not equal to operator compares two values and returns true if values are not equal as well as data type is not equal otherwise returns false.

var number1=100; //this is number
var number2=”10″; //this is String
if(number1!==number2)
{
document.getElementById(‘result’).innerHTML=”numbers are not equal as well as data type is not equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are equal”;
}

var number1=100; //this is number
var number2=10; //this is number
if(number1!=number2)
{
document.getElementById(‘result’).innerHTML=”numbers are not equal”;
}
else
{
document.getElementById(‘result’).innerHTML=”Both numbers are equal or data type is equal”;
}

You may also like...