JavaScript Debugging

Debugging can be simply defined as a process of finding errors in a code before actual execution. A JavaScript code can be debugged using a built-in web browser debugger, in the following two ways:

  • Using console.log() method
  • Using debugger keyword

Using console.log() method

It is used to generate an error message on the console of the browser, in case of any error in the code. Example:

<!DOCTYPE html>
<html>
<body>
<script>
a = 2;
b = 5;
mul = a * b;
console.log(mul);
console.log(x);
</script>
</body>
</html>

Using debugger keyword

Using the debugger keyword, breakpoints are set in the code itself. In case of any error, the code execution stops at that particular line of code and then can be verified manually. Example:

<!DOCTYPE html>
<html>
<body>
<script>
a = 2;
b = 5;
mul = a * b;
debugger;
document.write(mul);
document.write(x);
</script>
</body>
</html>
Please follow and like us:
Content Protection by DMCA.com