Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Debugging with Browser DevTools and AI Assistant

Recommended resources

Welcome to the third lesson in our journey. In the last session, you successfully wrote code with your AI assistant to fetch data from a local JSON file and dynamically display it on your webpage. You've built a complete loop: data is requested, received, and rendered. But in web development, things don't always go according to plan. A simple typo or a server issue can result in a frustrating blank page.

This lesson is about becoming a digital detective. We will explore how to use your browser's built-in developer tools to diagnose what went wrong and why. Then, you'll learn how to take those clues and formulate precise prompts for your AI assistant to help you fix the problem. Mastering this workflow of diagnosis and AI-assisted repair is a critical skill that will give you the confidence to tackle a wide range of web development challenges.

Your First Line of Defense: The Browser Developer Tools

Every modern web browser comes with a powerful suite of developer tools, often called "DevTools." While they have many features, we'll focus on the two panels that are most critical for debugging data-driven applications.

  • The Console: This is your application's logbook. It's where JavaScript reports errors, warnings, and any messages you explicitly ask it to print (using console.log()). When your page is blank or not behaving as expected, the Console is always the first place you should look.
  • The Network Tab: This panel acts like an airport control tower, showing every single request your webpage makes to a server. You can see if your fetch request was successful, what data it received, or why it failed.

This short video from the Chrome team provides a great visual introduction to the debugging workflow and the layout of the DevTools panels.

Debugging JavaScript - Chrome DevTools 101

Watch the beginning of this video from Chrome for Developers, "Debugging JavaScript - Chrome DevTools 101".

Pay close attention to the overview of the DevTools interface, particularly the three main parts of the Sources panel which the presenter breaks down from this section. You'll see the file navigator, the code editor, and the JavaScript debugging pane.


Scenario 1: The Failed Fetch Request

This is one of the most common issues you'll encounter. You've written your fetch code, but no data appears. Let's explore a few reasons why this might happen.

Diagnosis: Using the Console and Network Tabs

Imagine you made a small typo in your code from the last lesson. Instead of fetching './data.json', you wrote './data.jso'.

  1. You load the page. It's blank.
  2. You open DevTools and check the Console. You'll see an error, likely something like GET http://127.0.0.1:5500/data.jso 404 (Not Found).
  3. You switch to the Network tab. You'll see the request for data.jso listed in red, confirming the 404 Not Found status.

This tells you immediately that the problem isn't your rendering logic; the browser simply couldn't find the file you asked for.

Another infamous fetch error is the CORS (Cross-Origin Resource Sharing) error. This happens when your script, running on one origin (e.g., http://127.0.0.1:5500), tries to fetch a resource from a different origin that hasn't explicitly granted permission. You mentioned this was an issue you encountered, and it's something we'll solve definitively when we move to a backend.

This screenshot shows a typical `TypeError: Failed to fetch` in the console. The reason is a CORS policy block: the `Access-Control-Allow-Origin` header is missing from the server's response, which prevents the browser from accessing the data.

This screenshot shows a typical TypeError: Failed to fetch in the console. The reason is a CORS policy block: the Access-Control-Allow-Origin header is missing from the server's response, which prevents the browser from accessing the data.

A crucial subtlety of the fetch API is that it only considers a network failure (e.g., you're offline) a "real" error. An HTTP error status like 404 Not Found or 500 Internal Server Error will not trigger the .catch() block in your fetch promise chain. The request technically completed, even if the server said "I couldn't find that" or "I had a problem."

The video "Learn Fetch API In 6 Minutes" explains this concept clearly.

Learn Fetch API In 6 Minutes

Watch this segment from Web Dev Simplified's video on the Fetch API.

Focus on the explanation of how fetch handles errors. The key part is from this timestamp, where the presenter demonstrates that a 404 error doesn't trigger .catch() and shows how to check the response.ok property to determine if the request was truly successful.

This means you should always check if response.ok is true before trying to parse the JSON.


Scenario 2: The Rendering Error

Sometimes, the fetch works perfectly, but the page is still blank or broken. The Network tab shows a 200 OK status, and you can even preview the JSON response to see the data arrived correctly. In this case, the bug is in the JavaScript code that handles the data.

You'll typically see a TypeError in the console, like Cannot read properties of undefined (reading 'name') or data.forEach is not a function. This tells you that you're trying to use a variable that either doesn't exist or isn't the data type you expect.

Diagnosis: Using the Debugger and Breakpoints

While you could add console.log() statements everywhere to inspect your variables, there's a much more efficient way: breakpoints. A breakpoint is a deliberate pause you place in your code. When the browser's execution hits that line, it stops, letting you inspect the state of your application at that exact moment.

The MDN Web Docs provide an excellent guide on debugging. We'll use it to understand how to apply this to our fetch scenario.

JavaScript debugging and error handling - MDN Web Docs

This article from MDN is a superb guide to the fundamentals of debugging JavaScript. It will walk you through a broken fetch example, similar to the ones we're discussing.

Start by reading the introduction to the browser console, which shows how an error is first identified. Then, read the section on The Console API, which shows how logging variables helps, but doesn't always solve the problem. Next, focus on the detailed section on using the JavaScript debugger. This is the most important part. It explains how to set a breakpoint, inspect the call stack, and examine variables in the "Scopes" panel to find the root cause of the error. Finally, read the short challenge section that prompts you to fix the problem yourself using the debugger.

As you read, try to follow along by creating a small test file yourself. Intentionally break your code from the last lesson—perhaps by misspelling a property name inside your displayProducts function—and use the debugger to catch the error.


Using Your AI Assistant as a Debugging Partner

Once you have identified an error and gathered clues from DevTools, you can turn to your AI assistant. Your proficiency with AI tools is a significant advantage, but the quality of the AI's answer depends entirely on the quality of your prompt.

The key is to provide specific, contextual information. Don't just say "my code doesn't work." Instead, use a structured approach. The "Explicit Context Setup" and "Debug with Simulation" techniques are particularly effective here.

This table outlines several powerful techniques for prompting AI code assistants. For debugging, "Explicit Context Setup," "Input/Output Examples," and "Debug with Simulation" are especially useful.

This table outlines several powerful techniques for prompting AI code assistants. For debugging, "Explicit Context Setup," "Input/Output Examples," and "Debug with Simulation" are especially useful.

Here's a workflow you can follow:

  1. State the Goal: "I am trying to fetch data from a JSON file and display it as a list."
  2. Describe the Error: Copy the exact error message from the DevTools Console. "I'm getting this error: Uncaught TypeError: products.forEach is not a function."
  3. Provide the Code: Paste the relevant JavaScript function. "The error happens on the first line of my displayProducts function. Here is the code: [...your function code...]".
  4. Provide the Data Context: Show what the data looks like. "My fetch call is successful and the Network tab shows that the products variable I'm passing to the function looks like this: [...paste a snippet of the JSON...]".
  5. Ask the Question: "Why am I getting this TypeError even though the data looks like a valid array?"

With this level of detail, the AI can almost instantly spot inconsistencies, such as if your JSON is actually an object containing an array (e.g., { "data": [...] }) instead of being a raw array.


Conclusion

Today, you've added a crucial layer to your development skills. You've learned not just how to build, but how to diagnose and repair. This systematic approach—from identifying the symptom to using tools for diagnosis and leveraging AI for solutions—is the core of modern, efficient web development.

Here are the main takeaways:

  • Systematic Debugging: Always start with the Console for errors, then check the Network tab for request issues.
  • Breakpoints are Your Best Friend: Use the Sources panel and breakpoints to pause code execution and inspect the live state of your variables. This is far more powerful than console.log.
  • Context is King for AI: To get a useful fix from an AI assistant, provide the error message, the relevant code, and the data context you gathered from DevTools.
  • Understand Fetch Quirks: Remember that fetch only fails on network errors. Always check response.ok to handle HTTP errors like 404 or 500.

You now have a robust workflow for building and debugging a data-driven application using local data. With this foundation, you are ready to address one of the core security and functionality goals of your project. In our next lesson, we will begin migrating your application from a local JSON file to a secure, cloud-based backend using Firebase Firestore. This will eliminate local development issues like CORS and set the stage for a professional deployment.