JavaScript can be hard to learn and everyone makes mistakes when writing it.
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. (it runs sequentially).
Every statement in a script lives in one of three execution contexts:
The first two execution contexts correspond with the notion of 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 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.
If you are anticipating that something in your code may cause an error, you can use a set of statements to handle the error. This is important because if the error is not handled, the script will just stop processing and the user will not know why. So exception-handling code should inform users when there is a problem.
ERROR OBJECTS
Error objects can help you find where your mistakes are and browsers have tools to help you read them.