# JavaScript Comparison Operators

• 2.4 comparison_operator

# JavaScript Comparison Operators

JAVA SCRIPT BASICS

Java script comparison operators

(00:00)

Hello guys, welcome to our series Java script. We are going to continue what we did in our previous segment. After arithmetic operators we are going to see comparison operator. Comparison operators perform comparison between two values.

We will generate new program, html and this will be for comparison, example comparison operator, save, comparison.html. (3 seconds gap) okay, this is our program, so we will take only these three things here, variables which we generated and the heading. Okay (4 seconds gap) comparison operator.

In comparison first is, is equal to (=), here we have to check, comparison means always checking whether two values are equal or not equal or greater than etc. whenever we perform comparison we use ’if’, we are going to learn about if in detail in our next segment, but right now we are performing comparison operator and for that purpose we will see little bit about ‘if’ and in next segment we will cover the detailed portion about ‘if’.

Here you can see value of x is 10 and y is 20, let’s say I write if, meaning of if is like if and then if then the value is this then the value the value is that, if we consider that so and so condition is true then only our so and so code gets executed. Let’s say I take if x==y, now whatever my value is in bracket then the code gets executed.

If x is equal to y then only execute this code, means if x and y are equal, double equals means to check whether both the values are same or not. What is single equal to? It is assigned operator, to assign value of 10 to x I have used single equal to, we are going to see what are assignment operators but here I am trying once value is assigned I have to check.

(03:01)

Single equal to is used to assign value and double equal to is used to check value, so if I write x==y , then here we will be checking the value of x and value of y is same or not, so to check this , let’s say if value is same then I want to print document.write”values are same”, apart from this we can also write anything, okay save and reload.

Here what is happening, value of x is 10 and value of y is 20, then these values are not same, that’s why 16th line did not get executed. Let’s say I take a value z=10, here I take equal to z, and here I write z so reload, so you can see x==z is true, means value of z and x are same. Guys whenever we have to check whether both the values are same or not that time we use double equals to which checks is equal to.

Next is triple equals to(===), is equal to and same type, so here you can see value of x is 10 which is a number, value of y is 20 which is also a number, if we have to check the value as well as type then we use triple equals.

Her I will write x==w and here I will write var w= 10, okay here I have intentionally take 10 as value of w which is string, value of x is 10 which is a number if I check x and w then you can see x==w, save and reload here you can see x and w are same, here we have to add a br tag, okay, x and w are same.

(05:53)

Means why did this condition got executed because we are just checking the value, as x = 10 as number and w=10 as string so values are same. If I have to check these both with type, i.e. I want to check whether the type and values both are same or not, then I will write triple equals, x===w, now here what will be performed? Here value and type both will be checked for x and w, I will save it and reload, so you can see here the line disappeared.

Means my condition is wrong, If I change the value to 10 as number okay, so x and w are same, as value is also same and data type is also same means both are same 10=10. Whenever we have to check value as well as type at that time which operator will we use? Triple equals, okay.

Next is not equal to, not equal to means if we have to check both the values are not same, so let’s say I write here, (4 seconds gap), this if condition and here I will use not equals to, value of y is different so here I will take x and y are not same, okay so here I am getting x and y are not same. So here the condition is getting fulfilled so the statement is getting printed.

Here you can see x is 10 and y is 20, so value of x and y are different, they are not equal, that’s this portion is getting executed. This was our not equal to, then comes not equal to with double equal to means here not equal to will also be checked and same type will also be getting checked.

So we will execute this, so I will write like this, this is condition is true, x and y are not same i.e. 10 and 20 are not same so x and y are not same. If I add double equal to and check the type also, so save and reload and my condition is getting executed.

But let’s say I convert value of y in string, this condition is right so save, so as you can see not equal to with type means, the value should not be same and the type should also be not same. Triple equals to means same value and same type, not equal equal means different value and different type. That’s why my this portion is getting executed.

Then comes our greater than, so here we will take, you can see 10 and 20 and here we have taken 10 again. Okay so we have to perform greater than, so here I will take y is greater than x, okay so if y is greater than x then our line should get printed, so save and reload, so yes y is greater than x condition is true.

Next in our slide is greater than or equal to, means to check if value is greater or equal. Here I can perform this between x and z because these both values are same. Let’s say here I am writing z greater than or equal to x, now we will reload this, as you can see here z is greater than and equal to x has been printed because value of z and x are same i.e. 10.

(10:05)

If change the value of z to 50 then in this case it will be greater or equal, here it is greater so execution is getting performed. You can see here two outputs have been removed so I will again change the value to 10 so the output which disappeared has returned again. So here you have seen greater than or equal to. Next is same, as we greater than now we will see less than.

So x is smaller than y, so I will write here ( 8 seconds gap) x is less than y, so here also my condition is true so value of x is less than value of y. If I repeat the same thing x is less than and equal to y, so here it will say less than or equal to, okay as my value of x is less than y, and this will check till it gets equal.

These were my comparison operators, greater than equal to, less than and less than or equal to. So these were our arithmetic and comparison operator, in our next segment we will continue from Bitwise operator.

Thank you.

( video duration-11 minutes 32 seconds)

## Recommended Courses

### Share With Friend

Have a friend to whom you would want to share this course?   ######    