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
  • 1
  • 8

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,936
  • 10
  • 60
  • 110
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
  • 8,555
  • 2
  • 31
  • 39