-1

When running this piece of code, the JS engine pushes three(), two(), one() to the call stack, and in this order. My question is: Does the string "I'm function ONE!!!" return after one() is popped off the stack OR after two() and three() are popped off the stack?

I'm under the assumption that "I'm function ONE!!!" gets returned right after one is popped off the stack, and NOT after every function is popped off the stack.

Can someone correct me if I'm wrong?

function one() {
  return "I'm function ONE!!!";
}

function two() {
  return one();
}

function three() {
  return two();
}

three();
Keith
  • 22,005
  • 2
  • 27
  • 44
Eug
  • 165
  • 1
  • 2
  • 11
  • What do you mean by "the string gets returned"? Goes like this: Enter three -> Enter two -> Enter one -> Return to two with string -> Return to one with string -> Return to global with string. Right? – Arash Motamedi May 08 '19 at 22:33
  • I edited my question to be more specific. I meant the string that was returned from function one(). – Eug May 08 '19 at 22:38

1 Answers1

-1

After going through my code with the debugger in the browser, I have come to the conclusion that "I'm function ONE!!!" gets returned after EVERY function (one, two, and three) is popped off the Call Stack.

So I might have been wrong that "I'm function ONE!!!" gets returned right after one() is called.

Eug
  • 165
  • 1
  • 2
  • 11
  • 1
    I think you was correct the first time, because if `one` doesn't get returned after `one()` is called, `two()` wouldn't be able to use the return from `one`, and that would just be silly.. :) – Keith May 08 '19 at 22:45
  • @Keith Hmmm, you make a good point. But when I tried running this piece of code in Chrome with the debugger set, "I'm function ONE!!!" gets printed to the console AFTER the Call Stack was empty. Which led me to believe that all functions that had to be popped off the stack for one() to return. Please correct me if I'm wrong. – Eug May 08 '19 at 22:50
  • Why would `"I'm function ONE!!!"` be logged to the console, your not console logging anything?. I'm not sure what your trying to solve, but remember Javascript UI is event driven, as such while Javascript is running some blocking code, the event queue does not run, so anything visual will be delayed, maybe it's this your referring too. – Keith May 08 '19 at 22:58
  • I'm not trying to solve anything. Per my question, it's the fundamentals of JS I'm trying to understand. And it does print to the console if you run the code in any browser console. Also, I wasn't referring to any events as the above code does not refer to any web API's. I hope that clears things up. – Eug May 09 '19 at 00:54
  • I know your not using any event's, I never said you did. I said if you block the UI,the event queue also gets blocked. And the code you posted, prints nothing to the console, I've even converted your code into a runable snippet. If I run it here in Chrome nothing gets sent to the console, as would be expected, as your code doesn't have 1 line of code that sends anything to the console. – Keith May 09 '19 at 08:51
  • I'm not sure how your comment is adding to further the discussion on getting to a solution. Copy paste the code into a browser console, it should print what is getting returned. – Eug May 09 '19 at 15:09
  • Fair enough,. I obviously just don't understand the problem, as such it's tricky for me to give you a solution. If you could make a simple snippet demonstrating the problem, then it's more likely somebody might be able to give you a solution. – Keith May 09 '19 at 16:14