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
  • 43
  • 2
  • 8

2 Answers2

0

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

Check out ternary operators.

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
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
  • 9,293
  • 2
  • 31
  • 41