Call stack

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.

  1. When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.

  2. Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.

  3. When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.

  4. If the stack takes up more space than it had assigned to it, it results in a “stack overflow” error.

The code above would be executed like this:

  1. Ignore all functions, until it reaches the greeting() function invocation.
  2. Add the greeting() function to the call stack list.

     Call stack list:
     - greeting
    
  3. Execute all lines of code inside the greeting() function.
  4. Get to the sayHi() function invocation.
  5. Add the sayHi() function to the call stack list.

     Call stack list:
     - sayHi
     - greeting
    
  6. Execute all lines of code inside the sayHi() function, until reaches its end.
  7. Return execution to the line that invoked sayHi() and continue executing the rest of the greeting() function.
  8. Delete the sayHi() function from our call stack list.

     Call stack list:
     - greeting
    
  9. When everything inside the greeting() function has been executed, return to its invoking line to continue executing the rest of the JS code.
  10. Delete the greeting() function from the call stack list.

    Call stack list:
    EMPTY
    

The JavaScript Call Stack - What It Is and Why It’s Necessary

Let us take a look at a code sample to demonstrate LIFO by printing a stack trace error to the console.

    function firstFunction(){
    throw new Error('Stack Trace Error');
    }

    function secondFunction(){
  firstFunction();
  }

 function thirdFunction(){
 secondFunction();
  }

 thirdFunction();

SQL

SQL

This is what we mean by “manage function invocation”.

JavaScript error messages && debugging

The first thing that indicates you that something is wrong with your code is the (in)famous error message that the one we saw just moments ago, it usually appears on your console (being developer tools of the browser, terminal or whatever else you are using).

For those already used to programming, reading an error message is like second nature, for everybody else, is something you learn either you like it or not so might as well talk a bit about each of them.

This is as simple as when you try to use a variable that is not yet declared you get this type os errors.

    console.log(foo) // Uncaught ReferenceError: foo is not defined

This is also a common thing when using const and let, they are hoisted like var and function but there is a time between the hoisting and being declared so when you try to access them a reference error occurs, the fact that this happens to let and const is called Temporal Dead Zone (TDZ).

    foo = 'Hello' // Uncaught ReferenceError: foo is not defined
    let foo

Like the name indicates, this types of errors show up when the types (number, string and so on) you are trying to use or access are incompatible, like accessing a property in an undefined type of variable.

    var foo = {}
    foo.bar // undefined
    foo.bar.baz // Uncaught TypeError: Cannot read property 'baz' of undefined

References:

Main page