JavaScript debugging techniques

 

JavaScript debugging

Programming code may contain syntax errors or logic errors. Many of these errors are difficult to diagnose. Generally, when programming code contains errors, nothing happens. No error message, you will not be where to search for errors. Search in the programming code (and fix) called error code debugging. Debugging is not easy. But fortunately, all modern browsers have built-in JavaScript Debugger. You can open and close the built-in debugger to force the error report to the user. Using the debugger, you can also set breakpoints (may stop location code execution), and examine variables in the code execution. Typically, in accordance with the operation steps bottom of this page, and F12 to activate the debugger in the browser, and select "console (Console)" menu in the debugger.

 

console.log () method

If your browser supports debugging, you can use console.log () to display the value in JavaScript Debugger window:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<script>
a = 5;
b = 6;
c = a + b;
console.log(c);
</script>
</body>
</html>

  

Set a breakpoint

In the debugger window, you can set breakpoints in JavaScript code. At each breakpoint, JavaScript execution will stop and let you check the JavaScript value. After checking the values ​​that you can continue to execute code (usually the play button).

debugger keyword

debugger keyword stops executing JavaScript, and calls (if available) debugging. This is the same set breakpoints in the debugger functions. If there is no available debugging, the debugger statement is invalid. After opening the debugger, this code will stop executing before executing the third line.
 
var x = 15 * 5;
debugger;
document.getElementById("demo").innerHTML = x;

 

The main browser debugging tools

Learn more JavaScript

 
 

Guess you like

Origin www.cnblogs.com/jc2182/p/11669547.html