Call stack
- A call stack is a mechanism for an interpreter to keep track of its place in a script that calls multiple functions.
- When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
- Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
- 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.
- If the stack takes up more space than it had assigned to it, it results in a “stack overflow” error.
- The JavaScript engine is a single-threaded interpreter comprising of a heap and a single call stack.
- The browser provides web APIs like the DOM, AJAX, and Timers.
- The call stack is primarily used for function invocation (call).
- Call stack is synchronous.
- In Asynchronous JavaScript, we have a callback function, an event loop, and a task queue.
- Call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call), it means that the last function that gets pushed into the stack is the first to be pop out, when the function returns.
- Temporarily store: When a function is invoked (called), the function, its parameters, and variables are pushed into the call stack to form a stack frame.
- The call stack maintains a record of the position of each stack frame.
- A stack overflow occurs when there is a recursive function (a function that calls itself) without an exit point.
- Types of Errors:
- Reference errors: when you try to use a variable that is not yet declared you get this type os errors.
- Syntax errors: when you have something that cannot be parsed in terms of syntax.
- Range errors: when you try to give javascript data invalid property.
- Type errors: when the types (number, string and so on) you are trying to use or access are incompatible.
- The easiest and simple way to debug your js code is to use console.log().
- Anything after that error will not be executed.
- Catch the errors so you can gracefully fallback to a default state of our application in case of an error.
- Tools to avoid runtime errors