-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
  • 21,578
  • 41
  • 164
  • 232

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
  • 85,780
  • 21
  • 159
  • 179