1

When a function invokes itself does an identical copy of the function get created to be executed? and if that so is the execution of the original function get paused for the duplicate to run in a single-threaded language like javascript

function pow(x, n) {
  return (n == 1) ? x : (x * pow(x, n - 1));
}

pow(2, 3); //8
josh
  • 91
  • 6
  • Add a `console.log(n)` before and after the call to `pow()` and at least the second question will be answered. – Andreas Jan 14 '21 at 08:40
  • They key is that JavaScript maintains a bunch of *stack frames* which store context about the current function call. When a new function is called, it adds a new *frame* to the *stack*. – Sumner Evans Jan 14 '21 at 08:41
  • Please search your title before asking – mplungjan Jan 14 '21 at 08:46
  • Also see: https://www.freecodecamp.org/news/understanding-the-javascript-call-stack-861e41ae61d4/ and https://felixgerschau.com/javascript-event-loop-call-stack/#call-stack – Sumner Evans Jan 14 '21 at 08:47

0 Answers0