I was killing time reading the underscore.string functions, when I found this weird shorthand:
function count (str, substr) {
var count = 0, index;
for (var
It's equivalent to:
if (index >= 0) {
count = count + 1;
}
&&
is the logical AND operator. If index >= 0
is true, then the right part is also evaluated, which increases count
by one.
If index >= 0
is false, the right part is not evaluated, so count
is not changed.
Also, the &&
is slightly faster than the if
method, as seen in this JSPerf.