0

Can anyone shed any light on this short JavaScript line of code? Not sure what it is doing, as the greater than symbol inside it seems counter-intuitive:

direction = currentImage > imageToGo ? 1 : -1;
AndrewY
  • 23
  • 7

2 Answers2

0

If currentImage is greater than imageToGo, direction is assigned 1. If not, it is assigned -1.

Check out ternary operators.

weltschmerz
  • 12,320
  • 9
  • 57
  • 109
0

It is shorthand for if-else condition or basically ternary operator.

So your code can be written as

if(currentImage > imageToGo){
   direction = 1;
}
else{
   direction = -1
}
Abhinav Galodha
  • 7,820
  • 2
  • 27
  • 37