Understanding the JavaScript Call Stack & JavaScript error messages

My reading notes for Code Fellows


Understanding the JavaScript Call Stack & JavaScript error messages

Understanding the JavaScript Call Stack

What is a ‘call’? A call is an invocation of a function

How many ‘calls’ can happen at once? Since the call stack is single, function execution is done one at a time, from top to bottom. It means the call stack is synchronous.

What does LIFO mean? Last In First Out: The last function that gets pushed into the stack is the first to be run when the function returns. If one were to have a stack of books, and they wanted to add red book to the stack, they would add it to the top, because adding it to the bottom of the stack would potentially cause the stack to fall down. If one wished to remove a book from the stack, again, they would choose to do so from the top of the stack. This last in, first out.

Draw an example of a call stack and the functions that would need to be invoked to generate that call stack.

A call stack is a to do list for ordered functions.

Invocation:

Function a (function b(function c()))

Return:

  1. function c()
  2. function b()
  3. function a()

What causes a Stack Overflow? If the stack takes up more space than it had assigned to it, it results in a “stack overflow” error.

JavaScript error messages

What is a ‘reference error’? Occurs when one attempts to use a variable without declaring the variable.

Fix: Declare the variable before the declaration is made.

What is a ‘syntax error’? Occurs when one has a statement whose syntax cannot be parsed.

Fix: Fix the syntax.

What is a ‘range error’? Occurs when the length of an object is not valid.

Fix: Decline to mutate variables

What is a ‘type error’? Occurs when the types of things are incompatible. i.e. a string not being compatible with a number.

Fix: Properly declare the thing you want to access

What is a breakpoint? In the debugger window, you can set breakpoints in the JavaScript code. At each breakpoint, JavaScript will stop executing, and let you examine JavaScript values. After examining values, you can resume the execution of code (typically with a play button).

What does the word ‘debugger’ do in your code? The debugger keyword stops the execution of JavaScript, and calls (if available) the debugging function. This has the same function as setting a breakpoint in the debugger. If no debugging is available, the debugger statement has no effect.