Assignment Operators in Javascript

Javascript Comparison operators is used to perform comparison operations. There are various types of comparison operators available to perform various types of comparison. Lets discuss in detail.

Equal Operator(==)

Equal operator (==) is used to compare two values, and if both values are same, then it returns true otherwise it returns false.

Example:

 

var num1=100;

var num2=100;

if(num1==num2){

alert(“Both numbers are same”);

}else{

alert(“Both numbers are not same”);

}

If you run above code, then the output would be, Both numbers are same. Here ‘if block’ compares num1 and num2 value and executes first block if condition matches. Here both numbers are same, so it executed the first block.

Example:

 

var num1=100;

var num2=102;

if(num1==num2){

alert(“Both numbers are same”);

}else{

alert(“Both numbers are not same”);

}

 

If you run the above code, the output would be: Both numbers are not same. Because both numbers are not same.

The same way we compared numbers, we can compare, string values also.

 

var name1=”rakesh”;

var name2=”suresh”;

if(name1==name2){

alert(“both names are same”);

}else{

alert(“both names are not same”);

}

 

The output of the above code would be: both names are not same. Because name1 and name2 both are different.

 

var name1=”rakesh”;

var name2=”rakesh”;

if(name1==name2){

alert(“both names are same”);

}else{

alert(“both names are not same”);

}

The output of the above code would be: both names are same. Because name1 and name2 both are same.

Equal Same Type Operator or Identical operator (===)

Equal of same type operator (===) is used to compare values of two variables. But wait, we have already equal operator (==) for this. Then why do we need equal of same type operator(===). The following example would clarify.

 

var num1=5;

var num2=”5″;

if(num1==num2){

alert(“both values are same”);

}else{

alert(“both values are different”);

}

 

Note: Here we have used equal operator (==). And also notice, num1 value is 5 (its integer value). But value of num2 is “5” (its string value).

Okay now guess, what would be the output of the above code.

You must be thinking, it would print, both values are different.

Now, run the above code and you would wonder to notice, it prints “both values are same”

Why this happened?

Because Browser Engine, understands the type of variable from its value. Here the value of num2 is “5” which is string but Browser engine understands it as integer value.

The solution is to use equal same type operator (===).

It not only compares value of two variables but also checks both datatype.

 

var num1=5;

var num2=”5″;

if(num1===num2){

alert(“both values are same”);

}else{

alert(“both values are different”);

}

 

Now see, in the above code, we have used equal same type operator.

The output of the above code would be “both values are different”

Not Equal Operator (!=)

‘Not equal operator’ is used to check whether the values of two variables are different.

 

var num1=10;

var num2=15;

if(num1!=num2){

alert(“both values are different”);

}else{

alert(“both values are not different”);

}

 

If we run the above code, it would give us: both values are different.

Note: != is known as not equal operator

Not Equal Same Type Operator(!==)

Not Equal same type operator (!==) is used to compare values of two variables along with both datatypes.

We have already discussed in equal same type operator, why we need ‘equal same type operator(===)’

 

var num1=100;

var num2=”100″;

if(num1!==num2){

alert(“both values are different”);

}else{

alert( “both values are same with same datatype”);

}

 

The output of the above code would be: both values are different. Because num1 value is 100 and it is type of integer. But num2 is 100 but it is type of string.

Less than operator (<)

Less than operator is used to compare whether value of one variable is less than the value of another variable.

 

var num1=5000;

var num2=6000;

if(num1<num2){

alert(“Num1 is less than Num2”);

}else{

alert(“Num1 is not less than Num2”);

}

The output of the above code would be, Num1 is less than Num2.

Less than equal operator(<=)

Less than equal operator is used to compare whether the value of one variable is less than or equal to value of another variable.

 

var num1=5000;

var num2=5000;

if(num1<=num2){

alert(“Num1 is less than or equal to Num2”);

}else{

alert(“Num1 is not less than or equal to Num2”);

}

 

The output of the above code would be: Num1 is less than or equal to Num2.

 

var num1=4000;

var num2=5000;

if(num1<=num2){

alert(“Num1 is less than or equal to Num2”);

}else{

alert(“Num1 is not less than or equal to Num2”);

}

 

The output of the above code would be: Num1 is less than or equal to Num2.

Greater than operator (>)

Greater than operator is used to compare whether the value of one variable is greater than the value of another variable.

 

var num1=6000;

var num2=5000;

if($num1>$num2){

alert(“Num1 is greater than  Num2”);

}else{

alert(“Num1 is not greater than Num2”);

}

The output of the above code would be: Num1 is greater than Num2

Greater than equal operator (>=)

Greater than equal operator is used to compare whether the value of one variable is greater than the value of another variable.

var num1=5000;

var num2=5000;

if(num1>=num2){

alert(“Num1 is greater than  or equal to Num2”);

}else{

alert(“Num1 is not greater than or equal to Num2”);

}

The output of the above code would be: Num1 is greater than or equal to Num2

var num1=7000;

var num2=5000;

if(num1>=num2){

alert(“Num1 is greater than  or equal to Num2”);

}else{

alert(“Num1 is not greater than or equal to Num2”);

}

The output of the above code too would be: Num1 is greater than or equal to Num2.