-1

What does the following mean?

    var iCurrentIndex = oCurrentQuestionModel.getProperty("/index"); ...(1)
    iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0; ...(2)

The var iCurrentIndex = false in setp 1 and in step 2 it gets assigned value 0. What does this representation mean can anyone explain me?

loki
  • 648
  • 18
  • 37

2 Answers2

0

Your step 2 can be rewritten like this:

if (iCurrentIndex) {
    ++iCurrentIndex;
} else {
    return 0;
}

You are using ternary operator, read about it here.

errata
  • 5,695
  • 10
  • 54
  • 99
0

Assuming

var iCurrentIndex = oCurrentQuestionModel.getProperty("/index");

returns an undefined or null or just false, then an increment can not take place.

Then you need a check if the returned value is truthy (a value which resolves to true, if casted to boolean), then just increment or assign zero to iCurrentIndex.

iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0;

The above conditional oparator uses a condition and evaluates either the part after ?, the then part of an if statement or the part after :, the else part of an if statement.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thank you. So is ternary operator preferred more over if else statements? – loki May 12 '17 at 12:25
  • @loki, actually, you make an increment **and** an assignment so it is not really a good example of using a conditional operator. – Nina Scholz May 12 '17 at 12:28