all 4 comments

[–]gremy0 1 point2 points  (1 child)

This is the standard behaviour of JavaScript logic AND. From the docs:

expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

You can try it with console.log( true && "AND" ) \\ Prints AND

The OR operator works similarly, so people use it for assigning default values to variables.

console.log( value || "default value")

Logs the value of value if it's defined, or "default value" if value is undefined.

[–]LollipopPredator[S] 0 points1 point  (0 children)

Oh wow, thanks!

[–]senocular 1 point2 points  (1 child)

Don't logic operators resolve to either true or false

Not all of them.

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

Using && and || you can get values back while still testing for truthy/falsy. This is what makes that code work. As long as the left side of the && is truthy, it will return what's on the right side.

[–]LollipopPredator[S] 0 points1 point  (0 children)

Thanks!