The use of JavaScript logical operators || and && in numbers, the priority of various operators in JavaScript, from high to low:
1, () has the highest priority
2, Unary operator ++ - !
3. Arithmetic operator first * / % and then + -
4. Relational operator > >= < <=
5. Equality operator == != = === !==
6. The logical operator is && followed by ||
Logical or operator || and logical AND operator &&, which returns a Boolean value of true or false.
a || b, a and b in a && b can be a value, a variable, an expression, so it can also be a number, because the number itself can be regarded as a (logical) value, an expression. The following are the calculation results of the logical operators || and && when a and b have different values:
Logical AND operation a&&b, if a is converted to a Boolean value, it is false ( If the result after the operation is false, null, ", 0, undefined, etc.), the return result is a; if a is converted into a Boolean value and is true, the result b is returned; if multiple values (including expressions) are connected using && , if each expression is true, the operation result of the last expression is returned. If at least one is not true, the operation result of the first expression that is not true is returned.
Logic The OR operation a||b, starting from the left, returns the operation result of the first expression that is converted to a Boolean value and is true. If all are false, the operation result of the last expression is returned.
P.S: && takes precedence The level is higher than ||, first operate && and then use the result of && operation to || operation
JavaScript logical operators|| and && are used in numbers
In JavaScript The priority of each operator, from high to low:
1. () has the highest priority
2. Unary operator ++ – !
3. Arithmetic operator first * / % and then + -
4. Relational operators > #Logical OR operator || and logical AND operator && return a Boolean value, the value is true or false
a || b, a and b in a && b can be one value. , a variable, an expression, so it can also be a number, because the number itself can be regarded as a (logical) value or an expression. The following are the different values of a and b. The logical operator || Calculation result of AND&&:
Detailed examples of usage techniques of javascript operators || and &&
The above is the detailed content of How to use || and && in javascript. For more information, please follow other related articles on the PHP Chinese website!