What is Javascript interrogation for?

Asked

Viewed 4,944 times

4

Citing an example:

         if(this.gap > 1 || !this.sorted)
         {
            this.sorted = true;
            this.gap = parseInt(this.gap / 1.3) == 0 ? 1 : parseInt(this.gap / 1.3);
            setTimeout(this.name + '.comb_sort_acending_order(0);', this.speed);
         }

That question mark (?) means what?

1 answer

5


Is a conditional operator.

Of Developers.mozilla.org:

Conditional operator

The conditional operator is the only Javascript operator that uses three operands. The operator can have one of two values based on a condition. The syntax is:

condicao ? valor1 : valor2

If condition is true, the operator will have the value of value1. Case otherwise it will have the value of value2. You can use the operator conditional anywhere it would use a standard operator. For example:

var status = (idade >= 18) ? "adulto" : "menor";

In the case of your example, if parseInt(this.gap / 1.3) == 0 then this.gapwill be 1, otherwise it will be parseInt(this.gap / 1.3).

  • Forget what I said before, really the writing is Javascript, however the rest was right XD. See you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.