JavaScript can be hard to learn and everyone makes mistakes when writing it. This chapter will help you learn how to find the errors in your code. It will also teach you how to write scripts that deal with potential errors gracefully.
The JavaScript interpreter uses the concept of execution contexts. There is one global execution context; plus, each function creates a new new execution context. They correspond to variable scope.
Every statement in a script lives in one of three execution contexts:
GLOBAL CONTEXT : Code that is in the script, but not in a function. There is only one global context in any page.
FUNCTION CONTEXT: Code that is being run within a function. Each function has its own function context.
EVAL CONTEXT (NOT SHOWN) Text is executed .
VARIABLE SCOPE The first two execution contexts correspond with the notion of scope:
Global scope .
FUNCTION-LEVEL SCOPE.
The javascript interpreter processes one line of code at time .
Each time a script enters a new execution context, there are two phases of activity:
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 can help you find where your mistakes are and browsers have tools to help you read them.
When there is an error, you can see all of this information in the JavaScript console I Error console of the browser.
;
some common examples of the kinds of errors you are likely to see. As you can tell, the errors shown by the browsers can be rather cryptic.
Now that you know what an error is and how the browser treats them, there are two things you can do with the errors :
1: DEBUG THE SCRIPT TO FIX ERRORS.
2: HANDLE ERRORS GRACEFULLY.
WHERE IS THE PROBLEM? WHAT EXACTLY 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.
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.
The JavaScript console is just one of severa l developer tools that are found in all modern browsers. When you are debugging errors, it can help if you look at the error in more than one browser as they can show you different error messages.
You can also just type code into the console and it will show you a result.
objects
arrays that contain other objects or arrays.
You can indicate that a breakpoint should be triggered only if a condition that you specify is met. The condition can use existing variables.
Each one is given its own code block.