I have been using MDN Docs – Logical Operators as a frame of reference to understand the logical AND operator.
I have understood most of these code examples especially the first 4 as shown here:
a1=true && true // t && t returns true
a2=true && false // t && f returns false
a3=false && true // f && t returns false
a4=false && (3 == 4) // f && f returns false
a5="Cat" && "Dog" // t && t returns Dog
a6=false && "Cat" // f && t returns false
a7="Cat" && false // t && f returns false
However I am having issue understanding a5, a6 and a7.
I am failing to understand how the two strings a5="Cat" && "Dog" are evaluating to true && true returns Dog
I am also failing to understand why the string “Cat” is evaluating to true as part of a6=false && "Cat" // f && t returns false
First of all lets look at a5:
Which returns
dog, the mdn-docs states that AND(&&):Since a non-empty string can’t be converted to false, it will return
dog, if you change the order ofdogandcat, it will ofcourse returncat.In a6 false, is false and thus it returns
falsebecause of this:In a7
catis true and thus it goes on to the next expression which is false, and thus returns false.