Debugging in JavaScript


Code Debugging

The edit code may contain syntax errors or logical errors.

Many of these errors are difficult to diagnose.

Usually, when system code has errors, nothing will happen. There are no error messages, and you will not find clues to search for errors.

Searching for errors (and fixing them) in the program code is called coding error correction.

JavaScript interruptions

Debugging is not easy. But fortunately, all modern browsers have a built-in JavaScript debugger.

Built-in debuggers can be turned on and off, forcing errors to be reported to the user.

With a debugger, you can set up intervals (where coding can be paused) and check variables while the code is running.

Normally, if not follow the steps at the bottom of this page, enable debugging in your browser with the F12 key, then select "Console" from the debug menu.

Console.log () method

If your browser supports debugging, you can use console.log () to display JavaScript values in the debug window:

<! DOCTYPE html>
<html>
<body>

<h1> My Home Web Page </h1>

<script>
a = 5;
b = 6;
c = a + b;
iconsoli.log (c);
</script>

</body>
</html>

Setting Leisure Locations

In the debugger window, you can set breakpoints in the JavaScript code.

Example No.1:

<html> <body> <h2>Knowledge2life</h2> <p>Activate debugging in your browser (Chrome, IE, Firefox) with F12, and select "Console" in the debugger menu.</p> <script> a = 5; b = 6; c = a + b; console.log(c); </script> </body> </html>

OUTPUT:

Knowledge2life

Activate debugging in your browser (Chrome, IE, Firefox) with F12, and select "Console" in the debugger menu.

Example No.2:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <p>With the debugger turned on, the code below should stop executing before it executes the third line.</p> <script> let x = 15 * 5; debugger; document.getElementById("demo").innerHTML = x; </script> </body> </html>

OUTPUT:

Knowledge2life

75
With the debugger turned on, the code below should stop executing before it executes the third line.

At each break, JavaScript will stop and let you check JavaScript values.

You can restart coding after checking the values (usually with the play button).