I thought the following code will never reach to the console.log lines, because the next() function already run before to reach to the console.log lines and the if else condition with return prevent it too, but it is not so. Why?
var arr = ['a', 'b', 'c'];
var i = 0;
function next() {
if (i < arr.length) {
next(i++);
console.log('why go here 1:' + i); // 3,3,3
} else {
return;
}
console.log('why go here 2:' + i); // 3,3,3
}
next();
3? or why is it showing anything at all?iffrom within itself. If the conditioni < arr.lengthis met, both instructionsnext(i++)andconsole.log(...)will be executed regardless of the result of the first instruction.