When I use the logical opertor && between two strings, why does it give me the second string as output:
var c = "Cat" && "Dog";
in the expression
"Cat" && "Dog" // => "Dog"
Because you're using
true. In this case,
"Dog" is the just the last evaluated thing.
To be more explicit, you could do something like
var c = "Cat" != null && "Dog" != null
It's a little bit more wordy, but this boils down to
var c = true && true // => true
If you want a simple shortcut for the boolean
var c = !!("Cat" && "Dog") // => true
Per one of the comments below
true. Since "Cat" is true, it stops there and returns "Cat". This is known as Short-circuit evaluation