Tang JS Logic Judgment Selector || &&

Preface

|| Different from && in JS, this is a selector operator, not a logical operator.

For || For if it is determined to be true, the first value is returned, and the second value is returned for FALSE.
&& Instead, if it is determined to be true, the second is returned, and the first value is returned for false.
It is definitely not to return True or False like other languages, but will return the first value or the second value.

Simple test code

First TRUE and FALSE test, then change false to a value 0. Except for || Except for && In addition to the doubts, the fake value in JS is also a big doubt.

Let a = true; let b = true; testLogic (a, b); a = true; b = false; testlogic (a, b); a = false ; B = true; testlogic (a, b); a = false; b = false; testlogic (a, b); / * false value table, the following values ​​can be forced Boolerge to convert to false, other conversions are TrueundefineDnullFalse + 0 , -0, nan “” Empty string other is true, like empty object {} * / console.log (“————– Next Test — ———— “); a = true; b = true; testlogic (a, b); a = true; b = 0; testLogic (A, b); a = 0; B = true; testlogic (a, b); a = 0; b = 0; testLogic (a, b); Function Testlogic (A,B) {IF (a | | b) {Console.log (“A:”, A, “B:”, “. A || B TRUE:”, A || b);} else {console. LOG (“A:”, A, “B:”, B, “. A || B Flase:”, A || B);} IF (A && B) {Console.log (“A:”, A , “B:”, B, “. A && B True:”, A || B);} else {Console.log (“A:”, A, “B:”, “. A && B Flase:”, A && b);}}
  When I write code, I only know how IF and logic judgment implementing other languages. Normal effect:  
Conclusion is to ensure that A and B are TRUE or FALSE instead of other values.

泛谈JS逻辑判断选择器 || &&

A

B

A || B

T T T F T T f JS The language is this: Returns A or B, as long as the A and B are TRUE or FALSE instead of other values, then use the normal language logic. A B A || B A && B T
F
F F
A (t) B (t)


F T B (t) F F B (f) A (f)
The above is all the contents of this article, I hope to help everyone, I hope everyone supports Tumi Cloud.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment