-1

is part of an example of slideshow
I could not understand this part.
Who can explain me in detail:

        currentPosition = ($(this).attr('id')=='rightNav')
        ? currentPosition+1 : currentPosition-1;

how they would be able to write?(just to understand)

            currentPosition = ($(this).attr('id')=='rightNav')
            ? currentPosition+1 : currentPosition-1;
Scimonster
  • 32,893
  • 9
  • 77
  • 89
Maria
  • 1
  • 3
  • it is checking if the id value of the current element is equal to rightNav if yes then currentPosition variable will be equal to CurrentPosition + 1 if not equal then currentPosition will be equal to currentPosition - 1 – Robin Nov 10 '14 at 09:54
  • you can read about ternary operators in php http://php.net/manual/en/language.operators.comparison.php – Robin Nov 10 '14 at 09:56
  • 1
    @Robin You're linking to PHP documentation when the question is on JS? I know they do the same thing, but still... – Scimonster Nov 10 '14 at 09:59
  • sorry dude answered the question in hurry. – Robin Nov 10 '14 at 09:59

2 Answers2

2

It's the ternary operator, basically a short if.

It's equivalent is the folowing:

if ($(this).attr('id') == 'rightNav') {
    currentPosition += 1;
} else {
    currentPosition -= 1;
}
padarom
  • 3,529
  • 5
  • 33
  • 57
  • +1 for being the only answer to actually give the name of the operator. – Rory McCrossan Nov 10 '14 at 09:56
  • @Rory McCrossan: Actually that is the type of operator (i.e. it has 3 parts). The name of *that specific ternary operator* is the `conditional operator`. There just happens to only be one ternary operator in JS so the terms get misused. – iCollect.it Ltd Nov 10 '14 at 10:04
1

Its type of condition, it means, if condition is true, currentPosition will be incremented by 1, else decremented by one.

So it will be the same as:

if ($(this).attr('id') == 'rightNav') {
  currentPosition += 1;
}
else {
  currentPosition -= 1;
}
Legionar
  • 7,472
  • 2
  • 41
  • 70