Operation rules: If the first operator returns true, return the value of the second operator (not a Boolean value); if the first If an operator returns false, the first operator is returned, and the second operator is no longer evaluated (short circuit)
// &# 39; a' converted to boolean is true, so directly return the second operator '' & # 39; a & # 39; & & & # 39; & # 39; // & # 39; & # 39; converted to boolean is false, so return the first operator directly & # 39; & # 39; '' && 'a'
Or operator (||)
Or Operators are also used for the value of multiple expressions
Operation rules: If the Boolean value of the first operator is true, then return the value of the first operator directly; if the first operator If the Boolean value of an operator is false, then return the value of the second operator
// Boolean value of '' is false, so here returns 'b' & # 39; & # 39; || & # 39; b & # 39; // The boolean value of 'b' is true, so here returns 'b' & # 39; b & # 39; || & # 39; & # 39;
The or operator commonly used to set a default value for a variable
Ternary operator
expression1 ? expression2 : expression3;
If the Boolean value of expression1 is true, return expression2; If the Boolean value of expression1 is false, return expression3.
Recommended tutorial: js introductory tutorial
The above is the detailed content of how to use Boolean operators in js, please pay attention to other related articles on 1024programmer.com for more!