This is the seventh day of my participation in the August Genwen Challenge.More challenges in August
The operator
Operators include mathematical operators (addition, subtraction, multiplication and division), bitwise operators, relational operators, and so on. Operators can be used for various data types, such as strings, values, booleans, and so on, as well as for objects. When assigning an operator to an object, you usually call the previously mentioned valueOf() and/or toString() methods to get a value that can be computed.
Boolean operator
Logic is not
Logical non-operators use (!) And follow these rules:
- Operation object, return false
- Operates on an empty string, returning true
- Operates on a non-empty string, returning false
- Operand value 0, return true
- Operating on non-zero values, returns false
- Operation null, return true
- Operation NaN, return true
- Operation undefined, return true
Use two (!!) The result is the same as the result of calling the transformation function Boolean().
Logic and
The logic and operator, represented by (&&), operates on two numbers and returns true only if both are true. However, if the operand is not a Boolean, the following rule is met: The first operand is an object; the second operand is an object; the first operand is an object only if the first operand evaluates to true and both operands are objects; the first of the three operands is null, undefined, or NaN.
console.log(null && undefined); / / returns null
console.log(NaN && null); / / returns NaN
console.log(undefined && NaN); / / returns undefined
Copy the code
Logic or
The Boolean or operator (| |), said only two Numbers as false returns false, and follow these rules
- Returns the first operand if the first operand is an object
- If the first operand evaluates to false, the second operand is returned
- If both operands are objects, return the first operand
- If both operands are null, null is returned
- If both operands are NaN, NaN is returned
- If both operands are undefined, undefined is returned
Logic is often used to avoid assigning a variable to null or undefined
let myObject = preferredObject || backupObject;
Copy the code