JavaScript debugging

JavaScript Debugging

When writing JavaScript, it will be a pain without debugging tools.

JavaScript Debugging

It is difficult to write JavaScript programs without debugging tools.

Your code may contain syntax errors and logic errors. Without debugging tools, these errors are difficult to find.

Usually, if there is an error in JavaScript, there will be no prompt message, so you cannot find the location of the code error.



Often, errors will occur when you write new JavaScript code.


JavaScript Debugging Tool

Finding errors in program code is called code debugging.

Debugging is hard, but luckily, many browsers have built-in debugging tools.

The built-in debugging tools can be started or closed, and serious error messages will be sent to the user.

With debugging tools, we can set breakpoints (where code stops executing) and detect variables while the code is executing.

To enable the debugging tool in the browser, generally press the F12 key and select "Console" in the debugging menu.

QQ图片20161025145433.png

Set breakpoints

In the debug window, you can set breakpoints on JavaScript code.

At each breakpoint, the execution of JavaScript code will stop so that we can check the value of JavaScript variables.

After the check is completed, the code (such as the play button) can be re-executed.

debugger keyword

debugger keyword is used to stop executing JavaScript and call the debug function.

This keyword has the same effect as setting a breakpoint in the debugging tool.

The debugger statement will not work if no debugging is available.

Enable debugger and the code stops executing before the third line.

开启调试工具,在代码执行到第三行前会停止执行。var x = 15 * 5;
debugger;
document.getElementById("demo").innerHTML = x;
Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <head> </head> <body> <p id="demo"></p> <p>开启调试工具,在代码执行到第三行前会停止执行。</p> <script> var x = 15 * 5; debugger; document.getElementById("demo").innerHTML = x; </script> </body> </html>
submitReset Code