JavaScript logical & and && operators

Hello everyone, in this article we will learn logical && and & operators. It is not language dependent. it is just logic and optimization.

& and && both are called logical AND operator. Difference between them is in the way they are executed. The syntax for && and & is same.

Syntax:

Exp1 && Exp2 && Exp3 && ... ExpN

Exp1 & Exp2 & Exp3 & ... ExpN

Here Exp is Logical expression which returns either true or false.

Example:

A && B Results False && False => False False && True => False True && False => False True && True => True

It means if all expression returns true then end result will be true other wise false.

So what makes && and & different.

In case of && if first expression return true then it will execute next statement otherwise it will returns false. i.e. no more expressions will be further executed.

while in case of & all expression must be executed one by one.

let take an example to clear it more.

Validate and given number is not empty and numeric only.

Create three methods IsNotEmpty returns true if given number will not empty otherwise false.

IsNumber returns true if given number is of numeric type otherwise false.

IsValid returns true if given number is either not empty and numeric type.

const nl = '<br />'; var IsNotEmpty = function(a){ document.write('IsNotEmpty invoked! returns '+!(a=='') + nl); return !(a=='') } var IsNumber = function(a){ document.write('IsNumber invoked! returns '+!isNaN(a) + nl); return !isNaN(a); } var IsValid = function(n){ if(IsNotEmpty(n) && IsNumber(n))  document.write(n+' is Valid Number'+nl); else document.write(n+' is InValid Number'+nl); } var n1 = ''; var n2 = '34'; document.write('------------------------------------'+nl); document.write('Validate Number: '+n1+nl); IsValid(n1); document.write('------------------------------------'+nl); document.write('Validate Number: '+n2+nl); IsValid(n2);

Explanation:

When IsValid(n1); invoked IsNotEmpty() returns false and hence due to && logical AND operator, no more logical operation performed. only one method is invoked.

While for IsValid(n2); IsNotEmpty() returns true and next operation i.e. IsNumber() invoked which returns true. as shown in below result.

OUTPUT: ------------------------------------ Validate Number:  IsNotEmpty invoked! returns false is InValid Number ------------------------------------ Validate Number: 34 IsNotEmpty invoked! returns true IsNumber invoked! returns true 34 is Valid Number

Now lets modify if(IsNotEmpty(n) && IsNumber(n)) to if(IsNotEmpty(n) & IsNumber(n))

const nl = '<br />'; var IsNotEmpty = function(a){ document.write('IsNotEmpty invoked! returns '+!(a=='') + nl); return !(a=='') } var IsNumber = function(a){ document.write('IsNumber invoked! returns '+!isNaN(a) + nl); return !isNaN(a); } var IsValid = function(n){ if(IsNotEmpty(n) & IsNumber(n))  document.write(n+' is Valid Number'+nl); else document.write(n+' is InValid Number'+nl); } var n1 = ''; var n2 = '34'; document.write('------------------------------------'+nl); document.write('Validate Number: '+n1+nl); IsValid(n1); document.write('------------------------------------'+nl); document.write('Validate Number: '+n2+nl); IsValid(n2);

Explanation:

When IsValid(n1); invoked IsNotEmpty() returns false and due to $ logical AND operator, next operation i.e. IsNumber() invoked which returns true.

While for IsValid(n2); IsNotEmpty() returns true and next operation i.e. IsNumber() invoked which returns true. as shown in below result.

OUTPUT: ------------------------------------ Validate Number:  IsNotEmpty invoked! returns false IsNumber invoked! returns true is InValid Number ------------------------------------ Validate Number: 34 IsNotEmpty invoked! returns true IsNumber invoked! returns true 34 is Valid Number

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories