Sitemap
Mbasamahmad

Mbasamahmad

Debugging JavaScript code in Chrome

6 min readSep 19, 2023

--

Press enter or click to view image in full size
Photo by Sigmund on Unsplash

Web developers need to know how to debug JavaScript code on Google Chrome. It enables you to locate and fix bugs in your code, improving the dependability of your web application.

Get Mbasamahmad’s stories in your inbox

Join Medium for free to get updates from this writer.

To grasp the debugging process, we should create a simple web application that takes a number as input and determines whether that number is prime or not. Let’s delve into building this application.

<!DOCTYPE html>
<html>
<head>
<title>Prime Number Checker</title>
</head>
<body>
<h1>Prime Number Checker</h1>
<label for="numberInput">Enter a Number:</label>
<input type="text" id="numberInput">
<button onclick="checkPrime()">Check Prime</button>
<p id="result"></p>

<script src="Debugging.js"></script>
</body>
</html>

We are creating a simple web page for a Prime Number Checker web application in the above code.

  • There’s an input field where users can enter a number. This input field has an id attribute set to numberInput, which can be used to uniquely identify it.
  • Below the input field, there’s a “Check Prime” button. When this button is clicked, an action is triggered.
  • Below the button, there is an empty paragraph element with an id attribute set to result is provided. This paragraph will be used to display the result of whether the entered number is prime or not.

The HTML file includes an external JavaScript file named Debugging.js using the <script> tag. This JavaScript file likely contains the code for checking whether the entered number is prime or not. The code execution is linked to the button’s onclick attribute.

function isPrime(number) {
if (number < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false;
}
}
return true;
}

function checkPrime() {
const numberInput = document.getElementById("numberInput").value;
const resultElement = document.getElementById("result");
const number = parseInt(numberInput);// Convert input to a number
console.log("For the testing of console ");
if (!isNaN(number)) {
if (isPrime(number)) {
resultElement.textContent = `${number} is a prime number.`;
} else {
resultElement.textContent = `${number} is not a prime number.`;
}
} else {
resultElement.textContent = "Please enter a valid number.";
}
}

To check number is prime or not, we define two functions: isPrime(number) and checkPrime() For the Prime Number Checker application. Let's break down what each function does:

isPrime(number):

  • This function takes a single argument number to check whether it is prime or not.
  • It first checks if the number is less than 2. If it’s less than 2, the function immediately returns false because prime numbers are greater than 1.
  • Then, it enters a loop that starts from 2 and goes up to the square root of the input number. This loop checks if the input number is divisible by any integer within this range.
  • Inside the loop, it checks if the input number modulo i is equal to 0. If this condition is met for any i, it means the number has a divisor other than 1 and itself, so it's not prime, and the function returns false.
  • If the loop completes without finding divisors, the function returns, indicating that the input number is prime.

checkPrime():

  • This function is called when the Check Prime button in your HTML form is clicked.
  • It first retrieves the value entered in the input field with the id "numberInput" using document.getElementById("numberInput").value and stores it in the numberInput variable.
  • It also retrieves the HTML element with the id "result" and stores it in the resultElement variable. This element will be used to display the result.
  • It then converts the numberInput value to a number using parseInt() and stores it in the number variable.
  • It checks if the conversion was successful using isNaN(). If number is not a valid number, it displays "Please enter a valid number" in the resultElement.
  • If number It is a valid number and calls the isPrime(number) function to check if it's prime.
  • Depending on the result of the isPrime() function, it updates the textContent of the resultElement with either "${number} is a prime number." or "${number} is not a prime number."

Open Developer Tools

You need to open the Google Chrome browser to debug the JavaScript code. After the browser is launched, you need to open the developer’s tools with the help of the following way:

  • Right-click on the webpage and select “Inspect”.
  • Press Ctrl+Shift+I on Windows to open Developer Tools.
  • Press Cmd+Option+I on macOS to open Developer Tools.
Press enter or click to view image in full size
Developers tools view

Navigate to the Sources tab

Once you open the Developer Tools, go to the Sources tab. In the left panel, you'll find a list of various files that make up your code.

Press enter or click to view image in full size
File selection

Set Breakpoints

To debug a JavaScript file using the Chrome browser’s Developer Tools, first, locate the JavaScript file you want to debug in the left sidebar, usually under file:/// or the domain name of the website you're on.

  • Click on the file to open it, then navigate to the specific line of code where you want to set a breakpoint.
  • Click on the line number to set a breakpoint, indicated by a red dot, and this action will pause the execution of your code when that line is reached, allowing you to inspect variables and step through your code for debugging purposes.
Press enter or click to view image in full size
Add breakpoint

Start Debugging

  • Reload the webpage (F5 or Ctrl+R / Cmd+R) or trigger the JavaScript code execution.
  • The execution will pause at the breakpoint, and the Debugger panel will appear.
Press enter or click to view image in full size
Execution stop due to break point

You can deactivate breakpoints by clicking the breakpoint icon or removing them by right-clicking and selecting Remove breakpoint.

Debugger Panel

In the Debugger panel, you'll find controls to step through your code, including:

  • Step Into (F11 or Cmd+;): Go into functions.
  • Step Over (F10 or Cmd+'): Execute the current line and move to the next line.
  • Step Out (Shift+F11 or Cmd+Shift+;): Finish executing the current function.
  • Continue (F8 or Cmd+Alt+;): Resume code execution until the next breakpoint.
Press enter or click to view image in full size

Inspect variables and expressions

  • While paused, you can inspect variables and expressions in the Scope and Watch sections of the Debugger panel.
  • Hover over variables in your code to see their current values.
Press enter or click to view image in full size

Use Console for Logging and Evaluating Code

The Console tab in Developer Tools allows you to log messages and evaluate expressions in the current context.

Press enter or click to view image in full size

Once you’ve identified and resolved the issue, you can continue executing the code until it completes or stops. When you’re done debugging, close the Developer Tools panel by clicking X in the top-right corner or pressing Ctrl+Shift+I again. Debugging JavaScript in Chrome is a powerful way to diagnose and fix issues in your web applications, making it an indispensable tool for web developers.

--

--