JavaScript Debugger
Posted by Superadmin on May 03 2023 02:26:06

JavaScript Debugger

By Priya PedamkarPriya Pedamkar
  

JavaScript-Debugger

Introduction to JavaScript Debugger

Javascript debugger is used to find out coding errors in the program. While we are writing some new programming code, there may be a chance to encounter some errors. These errors may be syntactical or logical. It is very difficult to find out where the error is all the time because if I have 10 thousand lines of code, is it easy to analyze that where the error occurs manually? No right. In this kind of situation, we have a feature called debugging in any programming language.

How does Debugger Work in JavaScript?

In JavaScript debugging can be achieved in 2 ways:

1. By Using the Debugger Keyword

Note: To open a console in browsers like Chrome, Firefox, Internet Explorer, etc. press F12 or right-click on the page and click on inspect menu item then go to the console tab.

Console Tab:

Console Tab

Syntax:

var a=10;
debugger;
for(let i=0;i<=10;i++)
{
//code
}

Debugger mode looks like below:

JavaScript Debugger 1-2

When we used the debugger keyword in our code, if we press the F12 button and Run the code, we will get debugger mode as above. The first blue color button is used to Resume (come out of the debug) the code.

JavaScript Debugger 1-3

The second black color button is used to move step by step line execution of code.

2. By Using Breakpoints

In Eclipse IDE breakpoints look like below:

Breakpoints

You can see 1 and 3 lines have breakpoints with circles.

Examples of JavaScript Debugger

The topic is about debugger so we will look into examples with debugger keyword:

Example #1 – Palindrome with Debugger Keyword

Code:

<!DOCTYPE html>
<html>
<body>
<font color="blue"><h1 align="center">Palindrome with Debugger</h1></font>
<script>
function palindromeOrNot()
{
var remainder,total=0,actualNumber;
var input=77877;
actualNumber=input;
while(input>0)
{
debugger;
remainder=input%10;
total=(total*10)+remainder;
input=parseInt(input/10);
}
if(actualNumber===total){
document.write(actualNumber+": is palindrome number ");
}
else   {
document.write(actualNumber+": is not palindrome");
}
}
palindromeOrNot();
</script>
</body>
</html>

Output paused at debugger:

JavaScript Debugger 1-5

Final Output:

Palindrome

Explanation:

Example #2 – Factors with Debugger Keyword

Code:

<!DOCTYPE html>
<html>
<body>
<font color="blue"><h1 align="center">Factors with Debugger</h1></font>
<script>
function factors()
{
var input=arguments[0],j=1;
document.write("factors of : "+input+" are ");
document.write("<br>");
while(j<=input)
{
debugger;
if(input%j==0){
debugger;
document.write(j);
document.write("<br>");
}
j++;//post increment operator
}
}
factors(10);
</script>
</body>
</html>

Output paused at debugger:

JavaScript Debugger 1-7

Final Output:

Final Output

Explanation:

Example #3 – Try, Catch and Finally with Debugger Keyword

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">TRY CATCH FINALLY WITH DEBUGGER</h1>
</font>
<script>
function doTryCatchFinally()
{
try
{
debugger;
var p=Date.parsing("January 1,2020");
}
catch(error)
{
document.write(error+"<br>");
}
finally
{
document.write("I can kill or close connection even any errors thrown in the code");
}
}
doTryCatchFinally();
</script>
</body>
</html>

Output paused at debugger:

JavaScript Debugger 1-9

Final Output:

JavaScript Debugger 1-10

Explanation:

Example #4 – Perfect Number with Debugger Keyword

Code:

<!DOCTYPE html>
<html>
<body>
<font color="blue">
<h1 align="center">Perfect number with Debugger</h1>
</font>
<script>
function perfectNumber(input=28)
{
var i=1,sum=0;
while(input>i)
{
debugger;
if(input%i==0){
sum=sum+i;
}
i++;
}
if(sum==input)
{
document.write(input+" is perfect number");
}
else
{
document.write(input+" is not perfect number");
}
}
perfectNumber();
</script>
</body>
</html>

Output paused at debugger:

JavaScript Debugger 1-11

Final Output:

JavaScript Debugger 1-12

Explanation: