# Javascript Logical Operator

• 2.6 logical_operator

# Javascript Logical Operator

JAVA SCRIPT BASICS

Java script Logical Operator

(00:00)

Hello guys, welcome to our series Java script, from our last two segments we are learning our operators, arithmetic, comparison and bitwise operator has already been conducted. So I hope whatever was taught to you during our sessions, you have practiced it, because operators like bitwise requires lots of practice and time to understand, they work at deep level as they work with the system, we need to learn binary language and all for that.

So I hope you have given it time and practiced it well, if you have any questions or queries regarding previous segment then tell us on forums.learnver.com we will revert back with solutions to your questions and queries as soon as possible.

We are going to continue our operators only, today we are going to study logical operator, so here in slide you can see the logical operators like, double AND&&, double OR|| and NOT!. So last time when we checked in our slide bitwise operators also AND NOT and OR operators are present, so what is the difference between bitwise AND, OR, NOT and logical AND, OR, NOT.

So in bitwise operations values are first converted in binary and then AND and NOT are applied on its binary values. Whereas logical operators don’t work like that, logical operators are applicable on our current value itself. For example when we took two values in bitwise x y and z with values 5 9 and 2 respectively, so whenever we performed operation on 5 and 9 that time it was not performed directly on 5 and 9, 5 and 9 were first converted in binary, and the binary values which we got, for example, you can see the file here, value of 5 was 0101 and value of 9 was 1001, so bitwise operations were performed on these binary numbers.

In logical this does not happen, in logical operators, operations are performed on actual values only. So in logical we have three AND OR and NOT, that we will perform practically. I will set here html and write a program example logical operator, in script we will write the usual values which we already took, x y and z and we will take the heading from here, okay.

(03:01)

Now here we will take true as value of x and value of y will be false, so we don’t need z right now so we will remove it, okay save as logical, this got saved and now we will run the program, this is our logical operators, okay. So our first logical operator is AND, meaning of AND is performing and operations between two values. In binary when we saw AND we saw when both the values are 0 we get 0 output but when both values are 1 that time we get 1 as output.

Almost same concept is applied in logical also, if AND here is zero means it is false, if we consider 0 as false and we consider 1 as true so false and false will give us false, false and true gives us false, these are our logical AND operators, true and false will also gives us false, but if both the values are true then we will get true. So these are our AND operations. If both the values are false or if either one of the value is false then our output generated will also be false.

In case of OR if either one of the value is true then the output will be true, so here if it is 0 0 then output will be false, if it is 0 1 then it will be true, if both values are false then only output will be false in logical operators. If either one of the output is true then OR operation will gives true as output, ( 6 seconds gap) okay so as you can see this is the difference between logical AND and logical OR.

Logical AND means we will almost get the same output as our binary operations but here we have to use logic, we have to work with true and false, so if either one of the value is false then our output will be false, if both the values are true then only we will get true as output in logical AND.

If one of the two values is true then our output will be true, but if both the values are false then our output will be false. If we see the same thing here, here we have taken x=true and y= false and we have taken logical operator also. For this we will use if statement, if x, document. write “ statement is true” means what is x? our x is true so here also it is true so this will be executed. Here I take x&&y means true and false, if I execute such statement, then you can see nothing has generated here.

(06:17)

But if I take x&&x , then the output is statement is true, means x is true AND means logical AND again my X is true, so true and true. So if we see here our 1 & 1 is getting executed that is true, so our output is true. If my condition is x&&x, means if both the values are true then only my statement will be truein case of AND.

Similarly, let’s say I take here document.write(9 seconds gap) if x ||y, means either one value of x or y, so my x is true and y is false, means true or false so let’s come here OR in binary true or false, means if either one of the value is true then our value will be true, statement is true. So here you can see statement is true means our true portion of if is executed then only when our condition is proved true.

So here taking both the values gave me true, and here taking one value as true proved my statement true. So this was our logical AND and this was our logical OR. In slide also we have taken in the same way that, we always need two inputs to perform logical AND, if one of the statement is true then only our condition will be fulfilled and our statement will be printed.

(08:02)

Let’ s say if both are true then our logical AND will be true, if one of the value is true then our logical OR will be true. Now comes our logical NOT, NOT means, this sign!, so you can see value of y is false, if want to make the condition true on the basis of y, how will I do that? By simply applying NOT before y. here in our explanation you can see, in binary use of NOT converts 0 to 1 and 1 is converted to 0.

So currently it is 0, means my y is false, so if want to change the false to true then I need to apply NOT operation before this. So NOT of y , i.e. NOT of false, means what is opposite of false? It is true. So NOT of y i.e. true so statement is true, if save and reload it, you can see my third statement is also true, i.e. NOT of y, statement is true i.e. x OR y, statement is true i.e. x && x.

So we have written our operations, so these are our three logical operations, whenever we create a web application, or when you are coding, that time widely used operators or comparison are logical operators, bitwise is not much used. Because most of the time we have to work on conditional basis, for example if we are filling out any form, let’s say if I have added birth date then my age is calculated on basis of birth date.

But if I want my pension and I want that at a particular age, so for that I will use my comparison or my logical operators, my birth date should be added and age should not be more than 60, means whenever I have to perform AND operation that time I will us &&. When I have to add more than one condition in our program and I want all of them to be fulfilled, and then only my code of line should be executed, so for that logical AND are very useful.

So these were our logical operators, so guys in our today’s segment we will keep it till here only, In next segment we will continue our same topic, till then keep practicing.

Good bye.

(video duration 10 minutes 39 seconds)

### Share With Friend

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