is logic of javascript && (and) and || (or) operators correct?

Saw a weird explanation for how these operators work in javascript. But when you think about them, they actually seem to be just an accelerated version of the traditional ‘and’ and ‘or’. Are there any holes in this logic?

Here’s the explanation I read of how these operators are handled:
assume A and B are both boolean values (or expressions that evaluate to a true/false condition)

A&&B: If A is true, output B, else, output A
A||B: If A is false, output B, else, output A

The way to tell if two logical operations are the same is to compare truth tables.

For example:


A    B    A && B     (if A, then B, else A)
0    0       0                  0
0    1       0                  0
1    0       0                  0
1    1       1                  1

Since you can see that both operations result in the same results for all values of A and B, then those two are logically equivalent. The comparison with || is left as an exorcise to the OP.

I don’t recall the details of the Javascript operators, but the descriptions may also be explaining what will be evaluated in an expression. In the case of &&, if A is false, the result is false, and the value of B may not be evaluated. If A is true, then B would have to be evaluated. If B is an expression containing a function or assigment, the order of evaluation would be significant in the process.

Again, this does not necessarily apply to Javascript. I’ll try it out later.

I think OP is referring to the fact that the Javascript expressions don’t evaluate to a boolean true/false. They evaluate to false/B.

In other words: console.log( false && ‘foo’ ); prints boolean false.
console.log( true && ‘foo’ ); prints ‘foo’.

But my JS is rusty and I may have this wrong.

This is called ‘shortcutting’ evaluation. It’s a long-recognized technique for program optimization. I think the earliest mention of it was in Grace Hooper’s Flow-Matic system (predecessor to COBOL) circa 1955.

Yes, Javascript is shortcutting the evaluation, and it definitely evaluates to the actual A and B values provided. The description is meant to convey these details of operation since different languages have different rules for the evaluation of such operators.

Do you have a link to the “weird explanation”? Now you’ve made me curious.

|| and && in JavaScript are pretty standard.

As mentioned, they both appear to be correct if both are boolean values and it’s a shortcut. If one is clever, one can use this sort of behavior to one’s advantage as well. If you have behavior like the A&&B example then you can use it to avoid having to do some types of nested tests to help condense the code and still keep it readable. A good example might be if you want to check to see if a file contains certain data, but you first have to make sure it exists. If it is going to evaluate both arguments before determining truth, you have to check if it exists first then check for the content to avoid throwing an error. But it’s much simpler if you can do the check for existence and for the content in the same if-statement if it doesn’t continue once it sees the file doesn’t exist.

It’s also great for A||B in cases where you might be checking for multiple conditions. Why bother to keep checking once you’ve found a true statement, since nothing thereafter will change the truth value of the statement.

So, as for whether it’s correct, I guess it depends on how you look at it, because not being aware of that behavior could get some unexpected results, but I also think it’s sort of expected you’d get weird results when trying to use a boolean operator on non-boolean values.