The order in which statements are executed can be complex; some tasks cannot complete until another statement or function has been run:
Function greetUser() { return 'Hello' + getName(); }
Function getName() { let name = 'Mahmoud'; return name; }
This script above creates a greeting message, then writes it to an alert box (see right-hand page). In order to create that greeting, two functions are used: greetUser () and getName () .
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.
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 v a ri ables object.
If a JavaScript statement generates an error, then it throws an exception. At that point, the interpreter stops and looks for exception-handl ing code.
Error objects can help you find where your mistakes are and browsers have tools to help you read them.
- Debug the script to fix errors
- HANDLE ERRORS GRACEFULLY