1
x = (x === lightImages.length - 1) ? 0 : x + 1;

I understand the rest of my code so I won't add it because that would be pointless. Basically I'm just unsure what the '? 0' does in this code, my friend coded it for me because I was struggling with the rest of the code and I'd like to know exactly what is does so I could use it again properly.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Cubic Flux
  • 13
  • 3
  • 1
    Possible duplicate of http://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript – Smit Feb 21 '17 at 13:10

1 Answers1

0

It's a ternary operator.

Basically if (x === lightImages.length - 1) is true x = 0, otherwise x = x + 1.

You can think of it as a compact if () {} else {} statement. What precedes ? is the if condition, what comes immediately after is result. Anything after the : is the else result.

See here for more info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Eamonn
  • 418
  • 3
  • 11