JavaScript logical operators

JavaScript There are three logical operators:

Non (!), And (&&), or (||)

One:! non-

! Values ​​can be used for a non-operational

(A), Boolean value! Non-operational

The so-called non-operation is a Boolean value negate operation.

1, one operation becomes true false, false becomes true

2, if a value is inverted twice, the value will not change.

 

 Non-Boolean values ​​negate operation, now becomes a non-Boolean Boolean value. So we can use this feature to convert other types of values ​​into a Boolean value.

Any value may be twice the non-operation (inverted), to convert it to a Boolean;

The same principle and use Boolean () function

 (B) for non-Boolean values! OR

Operation rule: If the two values are true, the latter value is returned.

If the two values ​​are false, the foregoing false.

 

 Second, and &&

&& may be performed "and calculating" value on both sides of the symbol, and returns the result.

(A), a Boolean ANDed

Operation rules:

1, as well as true, the result returns true only on both sides of the symbol;

/2

2, as long as there is a value of false, the result returns false;

JS belongs to a short circuit and, if the first value is false, and does not see a second value (because the results can be seen already). / 3

3, the first value is true, the second check value.

/4

4, the first value is false, the second value is not checked.

  (B), carried out on non-boolean AND operation &&

If the first value is true, the second value is returned

If the second value is false, the direct return of a value (original value is returned).

 

Three, || or

|| both sides of the symbol values ​​may be ORed and return the results.

(A), or the operation of Boolean values

Operation rules:

Both are false, returns false

There are a true, returns true

JS in "or" belonging to a short circuit or,

If the first value is true, the second is not checked.

Only the first is false, it will check the second.

(B), for non-Boolean or arithmetic values ​​||

Calculation rule: if the first value is true, then directly returns the first value;

如果第一个值是false,则直接返回第二个值;(无论第二个值是什么)

 

Guess you like

Origin www.cnblogs.com/nyw1983/p/11512181.html