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.
Tip: Often, errors will occur when you write new JavaScript code.
JavaScript Debugging Tools
Looking for 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.
console.log() method
If the browser supports debugging, you can use console. The log() method prints the JavaScript value on the debug window:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1>我的第一个 Web 页面</h1> <p> 浏览器中(Chrome, IE, Firefox) 使用 F12 来启用调试模式, 在调试窗口中点击 "Console" 菜单。 </p> <script> a = 5; b = 6; c = a + b; console.log(c); </script> </body> </html>
Run the program and try it
Set a breakpoint
In the debug window, you can set breakpoints in 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.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <head> </head> <body> <p id="demo"></p> <p>开启调试工具,在代码执行到第三行前会停止执行。</p> <script> var x = 15 * 5; debugger; document.getElementById("demo").innerHTML = x; </script> </body> </html>
Run the program and try it
Debugging tools of major browsers
Usually, to enable the debugging tool in the browser, press the F12 key and select it in the debugging menu "Console" .
The steps for each browser are as follows:
Chrome Browser
Open the browser.
Select the tool in the menu.
Select Developer Tools among tools.
Finally, select Console.
##Firefox Browser
http://www.getfirebug.com.
Install Firebug.
Internet Explorer browser.
Opera
Safari