What does shorthand "index >= 0 && count++" do?
Asked Answered
T

3

3

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 i = 0; i < str.length;) {
    index = str.indexOf(substr, i);
    index >= 0 && count++; //what is this line doing?
    i = i + (index >= 0 ? index : 0) + substr.length;
  }
  return count;
}

Legal: Think twice before using the function above without giving credit to underscore.string


I put the line alone here, so you don't waste time finding it:

index >= 0 && count++;

I have never seen anything similar to that. I am clueless in what is doing.

Taxiway answered 16/2, 2012 at 22:14 Comment(1)
possible duplicate of What does (myVar && foo()) mean in JavaScript?Crosspollination
A
7
index >= 0 && count++;

First part: index >= 0

returns true if index has a value that is greater than or equal to 0.

Second part: a && b

most C-style languages shortcut the boolean || and && operators.

For an || operation, you only need to know that the first operand is true and the entire operation will return true.

For an && operation, you only need to know that the first operand is false and the entire operation will return false.

Third Part: count++

count++ is equivalent to count += 1 is equivalent to count = count + 1

All together now

If the first operand (index >= 0) of the line evaluates as true, the second operand (count++) will evaluate, so it's equivalent to:

if (index >= 0) {
  count = count + 1;
}

JavaScript nuances

JavaScript is different from other C-style languages in that it has the concept of truthy and falsey values. If a value evaluates to false, 0, NaN, "", null, or undefined, it is falsey; all other values are truthy.

|| and && operators in JavaScript don't return boolean values, they return the last executed operand.

2 || 1 will return 2 because the first operand returned a truthy value, true or anything else will always return true, so no more of the operation needs to execute. Alternatively, null && 100 will return null because the first operand returned a falsey value.

Alvar answered 16/2, 2012 at 22:25 Comment(1)
I like the explaination of || and && operators in JavaScript don't return boolean values, they return the last executed operand. . Thanks!Barbabra
C
6

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.

Cherey answered 16/2, 2012 at 22:15 Comment(4)
@Taxiway Google's closure compiler also turns most if constructions in && statements. As for the speed, it's only slightly faster in Firefox. It's unlikely that you get any improvements in speed when using && instead of if, just for speed. Also note that JSLint complain about && instead of if.Cherey
In that case, I will keep coding like I do and let the compilers do the job. The last thing I want is to debug hours because of a code I am not very familiar withTaxiway
Keep in mind it's more than a matter of style: the expression as an && statement allows the result to be assigned or passed as an argument.Cantor
@RobW fix the link of "this JSPerf" -> remove ")."Ichinomiya
K
4

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.

Kaiser answered 16/2, 2012 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.