https://faroukibrahim-fii.github.io/reading-notes/
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 statement or function has been run.
The JavaScript interpreter processes one line of code at a time. Whenm a statement needs data from another function, it stacks (or piles) the new function on top of the current task.
Each time a script enters a new execution context, there are two phases of activity:
In the interpreter, each execution context has its own va ri ables object. It holds the variables, functions, and parameters available within it. Each execution context can also access its parent’s variables object.
If a JavaScript statement generates an error, then it throws an exception. At that point, the interpreter stops and looks for exception-handling code.
Now that you know what an error is and how the browser treats them, there are two things you can do with the errors.
DEBUG THE SCRIPT TO FIX ERRORS If you come across an error while writing a script (or when someone reports a bug), you will need to debug the code, track down the source of the error, and fix it.
HANDLE ERRORS GRACEFULLY You can handle errors gracefully using try, catch, throw, and finally statements.
Debugging is about deduction: eliminating potential causes of an error. Here is a workflow for techniques you will meet over the next 20 pages. Try to narrow down where the problem might be, then look for clues.
WHERE IS THE PROBLEM? First, should try to can narrow down the area where the problem seems to be. In a long script, this is especially important.
WHAT EXACTLY IS THE PROBLEM? Once you think that you might know the rough area in which your problem is located, you can then try to find the actual line of code that is causing the error.
The JavaScript console will tell you when there is a problem with a script, where to look for the problem, and what kind of issue it seems to be.
You can pause the execution of a script on any line using breakpoints. Then you can check the va lues stored in variables at that point in time.
If you set multiple breakpoints, you can step through them one-by-one to see where values change and a problem might occur.
Here are a selection of practical tips that you can try to use when debugging your scripts.
Here is a list of common errors you might find with your scripts.