0

Sorry if this is a super basic question, but I'm trying to understand how the following expression works. I believe the code uses lodash and backbone.

<%= i%2 ? ' some output ' : '' %>

From what I can determine, it seems to return true (which in this case, outputs some output) whenever i is an even number. Is it simply evaluating whether i is divisible by 2?

  • `%` is modulus in javascript. Modulus, if you are not familiar with it, will give you the remainder portion of a division equation. So in this case, the modulus of 2 is either 0 or 1. And given that 0 is falsy and any other number is truthy, this allows the ternary conditional to only return the string if the value is truthy, or in this case, an odd number (or zero). – Taplar Sep 13 '18 at 22:50

1 Answers1

0

i%2 can be 1 or 0

when evaluating it as boolean 1=true, 0=false

so if tru 'some output' else ''

ivan.rosina
  • 250
  • 2
  • 8