1. Overview
|| (or) and && (and) are logical operators. But or/with the "logical operator" is not appropriate, called " operand operator " more appropriate!
Because | | (or) and && (and) are not Boolean values, but one (and only one) of the two operands.
2. Which operand is returned?
(1) a| | B
a| | b equivalent to a?a:b
(2) A&&b
A&&b equivalent to a?b:a
3. Application
(1) | | App--Set default values
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <title>|| Application</title> </Head> <Body> <Scripttype= "Text/javascript"> functionFoo (A, b) {a=a|| 'Hello'; b=b|| ' World'; Console.log (A+ ' ' +b); } </Script> </Body></HTML>
(2) && application--"Guardian operator"
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <title>&& applications</title> </Head> <Body> <Scripttype= "Text/javascript"> functionFoo (a) {Console.log (a); } vara= 3; //Foo is only called when a condition is determined to be truea&&foo (); </Script> </Body></HTML>
JavaScript logical operators (operand operators)