Errors can typically be classified into one of the following:
- Syntax or interpretation errors
- Runtime exceptions
- Incorrect logic
Syntax Errors
These include mismatched or missing quotes, parentheses and braces, incorrect case, spelling mistakes, illegal characters, and more. These can usually be caught easily bystatic code analyzers such as JSHint [2] and JSLint [3]. These tools analyze your code before it is executed on a web page and point out syntax and other common errors before you make your code live on your web site or application.
Runtime Exceptions
These are the errors that occur when your JavaScript code executes. Such errors can be triggered by referring to an undefined variable, dividing by zero, by a failed “assert” statement, or by using a “throw” statement in your (or a JavaScript library’s) code to manually specify that an exception has occurred.
When a runtime exception occurs, any JavaScript code after that line of code is not executed. Hence, if runtime exceptions are not caught and handled appropriately, they can leave your web page or application in an unexpected state.
You can catch and handle runtime exceptions by wrapping your code (that may throw an exception) with “try {}” and specifying the handler with “catch(exception) {}”
try {
var v = dummy; // causes a runtime exception
} catch(e) {
console.log(e.message); // “dummy is not defined”
}
In most browsers, you can also specify a global handler for all exceptions that have not been caught using a “try… catch” block, by defining window. onerror:
window.onerror = function(errorMessage, url, lineNumber) {
// ...
In Google Chrome, you can have DevTools pause JavaScript execution on all runtime exceptions by clicking the “pause” button at the bottom of the Sources panel. (You can launch DevTools by pressing Cmd-Opt-I (Mac) or F12 (Windows), or via Menu > Tools > Developer Tools.) To pause JavaScript execution on only uncaught runtime exceptions, you can click the “pause” button again. Clicking it again will disable pausing on exceptions.
Incorrect Logic
Incorrect logic in your code does not show any errors (e.g. in a JavaScript error console) but causes your code to not do what you intend it to. Debugging such errors requires some practice using debugging tools that your browser provides.
Debugging logic errors typically involves the following:
1. Logs and Asserts
Most browsers allow you to add logging statements to your code, to dump useful information about the execution of your JavaScript:
console.log(“Mouse coordinates: “ + evt.pageX + “, “ + evt.
pageY);
In Google Chrome, this output can be seen in the Consolepanel in DevTools. In addition to “log,” you can also use “warn,” “error,” and “debug.” These different categories can be filtered in the DevTools Console.
You can also use “console.assert()” to ensure that certain conditions or assumptions are being met in your code:
function processData(n, data) {
console.assert(n > 0, “n should be greater than zero”);
console.log(“Processing data”);
}
When the specified condition is not true, the corresponding message will be printed to the Console.
Both “console.error” and “console.assert” also dump the execution call stack. Execution does continue after the assert.
2. Breakpoints
You can also specify breakpoints on any lines in your JavaScript code in the browser’s debugger, e.g. in the Sources panel in Chrome DevTools.Similar functionality is available in all browsers. To specify a breakpoint on a line, you can click on its line number:
This will cause the debugger to pause the JavaScript when code execution reaches that line of code. This allows you to inspect the values of all variables, evaluate expressions, and inspect the call stack at that point in time. You can hover the mouse pointer over any variable name in the source text to see its value with the debugger paused.
You can see the call frames in the Call Stack side panel in the Sourcespanel. It shows the file names and line numbers for all the frames in the call stack. You can inspect the code and variable values at any of the call frames by moving to them and by clicking on them in the Call Stack side panel.
With the debugger paused, you can bring up the Console(by pressing the Escape key or moving to the Consolepanel in DevTools) and then evaluate any expression at that point in time. You have access to all variables in scope at the call frame selected in the Call Stackside panel (under Sources).
With the debugger paused on a line of code, you can step over the code line-by-line (e.g. by pressing F10 in DevTools) or step into the next function call (F11). You can also step out of the current function after executing the remainder of its code (Shift-F11) or continue execution by resuming the debugger (F8).
3. Watches
In addition to hovering the mouse pointer over variables to inspect their values, you can also set watchesto allow you to monitor the values of variables and expressions.
Once added to the Watchesside panel in the Sourcespanel, values of variables or expressions are kept up to date.
4. Conditional Breakpoints
If the breakpoint on a line gets hit to many times when you want it to break the execution only in specific cases, you can specify a condition on the breakpoint.
This can be done in DevTools by right-clicking on a line number and selecting “Add Conditional Breakpoint,” or right-clicking on an existing breakpoint and selecting “Edit Breakpoint.” You can then specify any expression as a condition for the breakpoint. The breakpoint will cause the JavaScript execution to pause only when the condition evaluates to true on that line of code.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}