Javascript Short Circuit (weird use of II / OR operator)

I came across this piece of code:

<a ng-click= "item.statusId !== itemStatus.in || transfer()"> 

I think we can generalize as:

 <element ng-click = "someVar !== someValue || doStuff()"> 

Then I found this article on short circuits , and the other more focused as soon as I realized what they are called. However, I still do not understand.

Does it mainly work on the principle of evaluating the end of an OR statement if the first statement evaluates to true? So, if the first statement is true, completion of the evaluation, if it is false, run the function in the second half of the OR statement? (This is the main question I ask, everything else is additional).

I assume that the part that I am not getting is that the compiler interprets this code differently or it still evaluates false and just runs the function. Not even sure how the phrase Q.

+5
source share
2 answers

Does it mainly work on the principle of evaluating the end of an OR statement if the first statement evaluates to true? So, if the first statement is true, complete the evaluation, if it is false, run the function in the second half of the OR statement?

It effectively shortens the if statement, and you correctly understood why it works. For more information on short circuit evaluation in JavaScript, see this stack overflow question .

+3
source

This code is equivalent to:

 //Note the intentional and explicit use of `== false` to mean falsy (NOT `=== false` to indicate strict equality test for false) if( (item.statusId !== itemStatus.in) == false ) { transfer(); } 

This is an interesting means of executing a function only when the condition is false.

There are many different examples of this around on StackOverflow, and does not apply to AngularJS.

+1
source

All Articles