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 the same as:
if(index >= 0){
count++;
}
JavaScript will evaluate the left side (index >= 0
), if it's false
the &&
(AND) will short circuit (since false AND anything is false), thus not running `count++.
If it's (index >= 0
) true
, it evaluates the right side (count++
), then it just ignores the output.