0

I'm wondering what the priority of the ++ operator is when being called through a recursive function like so.

var count = 0;
function recur(x){
    if(x == 10)
        return x;
    else
        return recur(x++);
}

recur(count);

In the code, when return recur(x++) is being called, is x passing to the recur method as x, or as x+1? And what is the difference between x++ and ++x?

Thanks.

James
  • 2,195
  • 1
  • 19
  • 22
mjkaufer
  • 4,047
  • 5
  • 27
  • 55

2 Answers2

2

With return recur(x++), it will return the first value and then will increase. return recur(++x) does exactly the opposite, value first increases and then returns. This is the difference x++, first run the command after increments, and ++x first increases then run the command.

1

Well the difference between x++ vr. ++x is:

var x = 5, y = 5;

console.log(++x); // outputs 6
console.log(x); // outputs 6

console.log(y++); // outputs 5
console.log(y); // outputs 6

So for ++x increments the variable inmediately.

adielhercules
  • 182
  • 1
  • 5