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;
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;
If currentImage
is greater than imageToGo
, direction is assigned 1. If not, it is assigned -1.
Check out ternary operators.
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
}