If x<10 the loop function doesn't return any value, that' why return loop(x + 1); gives you "undefined". As soon as you reach 10, the return x; statement kicks off and you get your returned value.
The fact that you use it as a recursive function doesn't make any difference here.