The front-end study notes (22) use logic and logical OR to assign values in JS

1. "||" logical OR

Logic or " True truth ", the first one is true or not, the second one is true or not, until the true value is returned, if there is no true value at the end of the comparison, then there is no way to return the last value

one example:

{} Behind to protect the bottom , there is a real guarantee can spit

var data = res.data || {};

2. "&&" logical and

The logical AND is " fuse ". The first one is continuously broken, and the second one is continuous. The comparison is continued until it is broken. If none is broken, the comparison is at the end and the last value is displayed.

one example:

It can replace if for fusing. If the first one is broken, the values ​​after it will not be executed and return false. If the first value is constant, the content after && will be assigned.

>var str = "123"
>var test = str.length > 0 && "zym";
console.log(`
---------test && -------
${
      
      test}
`);

Output result:
---------test && -------
zym

Guess you like

Origin blog.csdn.net/qq_42698576/article/details/108112200