Skip to the content.
Error Handling & Debugging
Order Of Execution
- To find the source of an error, it helps to know how scripts are processed.
- The order in which statements are executed can be complex; some tasks cannot complete until another staement or function has been run.
Execution Context
- The JavaScript interpreter uses the concept of execution contexts.
- There is one global excution context; plus, each function creates a new execution context. They correspond to variable scope.
- Execution Context
- Global Context
- Function Context
- Eval Context
- Variable Scope
- Global Scope
- Function-Level Scope
The Stack
- The JavaScript interpreter processes one line opf code at a time.
- When a statement needs data from another function, it stacks the new funmction on top of the current task.
Execution Context & Hoisting
- Each time a script enters a new execution context, there are two phases of activity;
- i. Prepare
- The new scope is created
- Variable, functions, and arguments are created
- ii. Execute
- Now it can assign values to variables
- Reference functions and run their code
- Execute statements
Understanding Scope
- In the interpreter, each execution context has its own variables object.
- It holds the variables, functions, and parameters available within it.
- Each execution context can also access its parent’s variable object.
Understading Errors
- If a JavaScript statement generates an error, then it throws an exception.
- At that point, the interpreter stops and looks for excception-handling code.
- Error objects can help you find where you mistakes are and browsers have tools to help you read them.
- SyntaxError
- ReferenceError
- EvalError
- URIError
- TypeError
- RangeError
- Error
- NaN
How To Deal With Errors
- Now that you know what an error is and how the browser treats them, there are two things you can do with the errors.
- i. Debug the script to fix errors
- ii. Handle errors gracefully