-1

How should we interpret the following code in JavaScript:

for (i = 0; i <= 20; i++)
    tests[i] && (buffer[i] = getPlaceholder(i));/* how this line interpreted */

I see that somewhere but I don't know what the inner code mean.

Mohammad Dayyan
  • 20,033
  • 39
  • 154
  • 219

1 Answers1

2
tests[i] && (buffer[i] = getPlaceholder(i));

The code is using logical AND operator. First the statement before && - tests[i] is executed and if that is truthy then only the statement after && - (buffer[i] = getPlaceholder(i)) is executed.

The code is equivalent as follow

if (test[i]) {
    buffer[i] = getPlaceholder(i);
}
Tushar
  • 82,599
  • 19
  • 151
  • 169