Home > Web Front-end > JS Tutorial > body text

How to use || and && in javascript

小云云
Release: 2018-03-17 11:54:14
Original
2099 people have browsed it

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:

  1. 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.

  2. 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&&:

Logical AND operation a&&b, if a is converted into a Boolean value and is false (the result after the operation is false, null, ", 0, undefined and other values) then 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 of the last expression is returned As a result, if at least one of them is not true, the operation result of the first expression that is not true is returned
  1. Logical OR operation a||b, starting from the left, returns the first one converted to The operation result of the expression whose value is true after the Boolean value. If all are false, the operation result of the last expression is returned.
  2. P.S: && has a higher priority than ||, first operate && and then use the result of && operation to | |Operation.

  3. Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!