Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does built-in functions add in a call stack?

Tags:

javascript

I am new in js and reading eloquent javascript book but the call stack part is confusing me. This is the sample code:

debugger;
function greet(who) { 
debugger;
  console.log("Hello " + who);
}
debugger;
greet("Harry");
debugger;
console.log("hi");  
console.log("bye");

This is what I have observed, debugger and console.log is an anonymous call. function greet gets defined in the global scope, but when I hit the line 7, there's still an anonymous call on the stack and function greet gets added on the stack? but why is there anonymous? can someone please tell me more about the call stack and what is happening here?

like image 697
gpbaculio Avatar asked Dec 04 '25 09:12

gpbaculio


1 Answers

All code that is top-level (not in a function) is automatically moved into an internal function by the JS engine. Your code would be converted to:

(function() {
    debugger;
    function greet(who) { 
        debugger;
        console.log("Hello " + who);
    }
    debugger;
    greet("Harry");
    debugger;
    console.log("hi");  
    console.log("bye");
})();

This is the anonymous function that is at the bottom of the call stack.

like image 82
MultiplyByZer0 Avatar answered Dec 07 '25 00:12

MultiplyByZer0