In JavaScript, a ‘call’ refers to the process of invoking a function. When a function is called, it is added to the call stack, which keeps track of where the program is in its execution.
JavaScript is single-threaded, meaning that only one call can be processed at a time. Calls are processed in the order they are added to the call stack, and each call must complete before the next one can be processed.
LIFO stands for “Last In, First Out”. In the context of the call stack, LIFO means that the most recently added call is the first one to be removed. In other words, the call stack operates like a stack of plates - the last one added is the first one removed.
Assume we have three functions, a()
, b()
, and c()
, where a()
calls b()
, and b()
calls c()
.
Call stack:
c()
b()
a()
A stack overflow occurs when the call stack becomes too large, typically due to an infinite or excessively recursive function call. When this happens, the browser or runtime environment will throw a “Maximum call stack size exceeded” error.
A ‘reference error’ occurs when you try to access a variable or function that doesn’t exist. This can happen if you misspell the name of the variable or function, or if it hasn’t been declared yet.
A ‘syntax error’ occurs when you write code that doesn’t follow the syntax rules of the JavaScript language. This can happen if you forget a semicolon, use the wrong type of quotes, or write an invalid keyword.
A ‘range error’ occurs when you try to use a value that is outside the range of acceptable values. For example, if you try to create an array with a negative length, or use a negative index to access an array element.
A ‘type error’ occurs when you try to use a value of the wrong type. For example, if you try to call a method on a variable that isn’t an object, or try to access a property on a string.
A ‘breakpoint’ is a point in your code where execution will pause when you run your program in a debugger. This allows you to step through your code line by line and inspect the values of variables at each step.
The ‘debugger’ statement is a keyword in JavaScript that tells the browser or runtime environment to pause execution at that point in the code. This allows you to inspect the call stack, variables, and other program state to help identify and fix bugs.