# Boolean operator

## Overview

Boolean operators are used to convert expressions into Boolean values. There are four operators in total.

-Negation operator: `!` -And operator: `&&` -Or operator: `||` -Ternary operator: `?:`

## Negation operator (!)

The negation operator is an exclamation point, which is used to change a Boolean value to the opposite value, that is, `true` becomes `false`, and `false` becomes `true`.

``````!true; // false
!false; // true
``````

For non-Boolean values, the negation operator turns them into Boolean values. It can be memorized like this, the following six values ​​are inverted and become `true`, and all other values ​​are `false`.

-`undefined` -`null` -`false` -`0` -`NaN` -Empty string (`''`)

``````!undefined; // true
!null; // true
!0; // true
!NaN; // true
!""; // true

!54; // false
!"hello"; // false
![]; // false
!{}; // false
``````

In the above code, no matter what type of value, after the inversion operation, it becomes a Boolean value.

If a value is inverted twice in a row, it is equivalent to converting it to the corresponding Boolean value, which has the same effect as the `Boolean` function. This is a commonly used way of writing type conversion.

``````!!x;
// Equivalent to
Boolean(x);
``````

In the above code, no matter what type of value `x` is, after two inversion operations, it becomes the same Boolean value as the result of the `Boolean` function. Therefore, negating twice is a convenient way to convert a value to a Boolean value.

## And operator (&&)

And the operator (`&&`) is often used to evaluate multiple expressions.

Its operation rule is: if the Boolean value of the first operator is `true`, then the value of the second operator is returned (note that it is a value, not a Boolean value); if the Boolean value of the first operator is ` false`, the value of the first operator is returned directly, and the second operator is no longer evaluated.

``````"t" && ""; // ""
"t" && "f"; // "f"
"t" && 1 + 2; // 3
"" && "f"; // ""
"" && ""; // ""

var x = 1;
1 - 1 && (x += 1); // 0
x; // 1
``````

In the last example of the above code, since and the Boolean value of the first operator of the operator is `false`, its value `0` is directly returned, and the second operator is no longer evaluated, so the variable` The value of x` has not changed.

This mechanism of skipping the second operator is called "short-circuiting". Some programmers like to use it to replace the `if` structure. For example, the following is a section of code of the `if` structure, which can be rewritten with the AND operator.

``````if (i) {
doSomething();
}

// Equivalent to

i && doSomething();
``````

The two ways of writing the above code are equivalent, but the latter is not easy to see the purpose, and it is not easy to debug. It is recommended to use it with caution.

And operators can be used in conjunction with multiple, then return the value of the first expression with a boolean value of `false`. If the boolean values ​​of all expressions are `true`, the value of the last expression is returned.

``````true && "foo" && "" && 4 && "foo" && true;
//''

1 && 2 && 3;
// 3
``````

In the above code, in Example 1, the first expression with a boolean value of `false` is the third expression, so an empty string is obtained. In Example 2, the boolean value of all expressions is `true`, so the value `3` of the last expression is returned.

## Or operator (||)

The OR operator (`||`) is also used to evaluate multiple expressions. Its operation rule is: if the Boolean value of the first operator is `true`, the value of the first operator is returned, and the second operator is no longer evaluated; if the Boolean value of the first operator is If the value is `false`, the value of the second operator is returned.

``````"t" || ""; // "t"
"t" || "f"; // "t"
"" || "f"; // "f"
"" || ""; // ""
``````

The short-circuit rule also applies to this operator.

``````var x = 1;
true || (x = 2); // true
x; // 1
``````

In the above code, the first operator of the OR operator is `true`, so it returns `true` directly, and does not run the second operator. Therefore, the value of `x` has not changed. This mechanism of controlling whether to run the second expression only through the value of the first expression is called a "short-cut".

The OR operator can be used in multiples, and then the value of the first expression with a Boolean value of `true` is returned. If all expressions are `false`, the value of the last expression is returned.

``````false || 0 || "" || 4 || "foo" || true;
// 4

false || 0 || "";
//''
``````

In the above code, in Example 1, the first expression with a Boolean value of `true` is the fourth expression, so the value 4 is obtained. In the second example, the boolean value of all expressions is `false`, so the value of the last expression is returned.

The OR operator is often used to set a default value for a variable.

``````function saveText(text) {
text = text || "";
// ...
}

// Or written as
saveText(this.text || "");
``````

The above code indicates that if no parameter is provided when the function is called, the parameter is set to an empty string by default.

## Ternary conditional operator (?:)

The ternary conditional operator consists of a question mark (?) and a colon (:), separating three expressions. It is the only operator in the JavaScript language that requires three operators. If the Boolean value of the first expression is `true`, the value of the second expression is returned, otherwise the value of the third expression is returned.

``````"t" ? "hello" : "world"; // "hello"
0 ? "Hello" : "world"; // "world"
``````

The boolean values ​​of `t` and `0` in the above code are `true` and `false` respectively, so the values ​​of the second and third expressions are returned respectively.

Generally speaking, ternary conditional expressions and `if...else` statements have the same expressive effect, the former can be expressed, and the latter can also be expressed. But there is a major difference between the two. `if...else` is a statement and has no return value; the ternary conditional expression is an expression and has a return value. Therefore, when you need to return a value, you can only use ternary conditional expressions instead of `if..else`.

``````console.log(true ? "T" : "F");
``````

In the above code, the parameter of the `console.log` method must be an expression, then only ternary conditional expressions can be used. If you want to use the `if...else` statement, you must change the entire code.