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 built-in web browser debugger, in 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 debugger keyword, breakpoint 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 Share