Any expression in Javascript is also a statement.
Expressions are things like:
1 + 3
a - b
4 + f(5)
Each of these expressions evaluate to a result, you don't have to capture it though. Just like you can throw a function's return value away by simply not assigning it anywhere with an '=', you can throw any expression's return value away. The following is valid:
function a() {
8 + 5;
"hello";
a == 3;
}
Admittedly this function isn't very useful, but it demonstrates how expressions' outputs can be thrown away.
Now, onto the example you provided...
The && operator is a logical AND. It takes two parameters (either side of it), and evaluates to either true or false. Typically you'd use it like this:
var result = f(4) && f(5);
This operator also has another aspect to its behaviour, though; it does 'short-circuit evaluation'. What does this mean? Consider what an AND actually does; it only returns true if both parameter are true.
If the first parameter is false, there's no point in checking the second one, because you already know that they aren't both true, and you know that your result will be false. 'Short-circuit evaluation' is when the runtime doesn't bother evaluating the second argument if the first argument already gives it everything it needs.
In the case of &&, if the first parameter is false, it will not bother evaluating the second parameter.
If the second parameter is the return value of a function, like a && b(3)
, b(3) WILL NOT be called.
If a is true, on the other hand, b(3) will need to be called to figure out if a && b(3)
is true.
So thanks to short-circuit evaluation, the logical AND actually behaves like an if-then, only evaluating the second parameter if the first one is true.
The opposite is true for the || (OR) operator, where the second parameter is only evaluated if the first is false (because true OR anything is always true).
Going back to the start of this answer now, where we talked about expressions, we see that the a && b()
expression can just be used standalone. We don't need to capture the result, we just want to use short-circuit evaluation to control whether or not b() is called. The eventual result, true or false, is thrown away.
More information on short-circuit evaluation