Skip to Content
Course content

3: Using the Browser Console and DevTools

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're a mechanic working on a modern car. You can't just look at the engine and know exactly why the fuel mixture is off; the engine is a sealed system. To really see what's happening inside while the car is running, you plug a diagnostic scanner into the OBD-II port. Suddenly, you have a screen showing you the real-time RPM, oxygen sensor levels, and specific error codes telling you exactly which cylinder is misfiring. You aren't just guessing; you're seeing the internal state of the machine in real-time.

Browser DevTools are that diagnostic scanner for your JavaScript. Your code is the engine, and the browser is the car. When your script doesn't work, you don't just stare at the source code and hope for the best—you plug into the console to see what the "sensors" are reporting.

Peeking Inside with the Console

The most common tool you'll use is console.log(). I know it seems basic, but it's the bread and butter of debugging. It's like putting a sensor on a specific wire to see if electricity is actually flowing through it at that exact moment.

Let's say you're building a shopping cart. You have a variable that's supposed to hold the total price, but for some reason, the checkout button is disabled. Instead of guessing, you drop a log in there:

const cartTotal = 45.99;
const taxRate = 0.07;
const finalPrice = cartTotal * taxRate; // Oops, I forgot to add the original total!

console.log("The final price is:", finalPrice); 
// The console will tell me: "The final price is: 3.2193"
// Now I immediately know my math is wrong.

I've spent hours of my career chasing bugs that a single console.log would have solved in ten seconds. Don't be too proud to use it.

Organizing Data with Tables

When you start dealing with arrays of objects—like a list of users or a product catalog—console.log becomes a cluttered mess of clickable arrows. It's tedious. Instead, I want you to use console.table(). It turns a messy array into a clean, sortable grid right in your browser.

const users = [
  { name: "Alice", role: "Admin", id: 1 },
  { name: "Bob", role: "Editor", id: 2 },
  { name: "Charlie", role: "User", id: 3 }
];

console.table(users);

Using table makes it instantly obvious if one of your objects is missing a property or if a value is undefined. It's a small shift in habit, but it saves a massive amount of mental energy.

Decoding the Red Text

When something goes catastrophically wrong, the console screams at you in red. This is the "error code" from our mechanic analogy. The most important part of an error message isn't the scary text at the top; it's the stack trace—the list of file names and line numbers underneath the error.

If you see Uncaught ReferenceError: x is not defined at script.js:15, the browser is literally pointing its finger at line 15 of your file. Go there first. Usually, it's just a typo or a variable you forgot to declare. I've learned to actually love these errors because they tell me exactly where the problem is, rather than letting me hunt for it.

Testing Ideas on the Fly

One of the coolest things about the console is that it's a REPL (Read-Eval-Print Loop). You can type JavaScript directly into the console and hit Enter to run it immediately on the current page.

If you're wondering, "Would this logic work if I used a while loop instead of a for loop?", you don't have to rewrite your file, save it, and refresh the page. Just type the logic into the console and see if it spits out the result you expect. It's a sandbox where you can fail fast and iterate quickly without breaking your actual codebase.




📋 Practical Task

Debugging the Broken Discount Calculator

You are working on a promotional page where users get a discount if their order is over $100. However, the current code is buggy, and the final price is coming out as NaN (Not a Number) or an incorrect value.

Your Task:

  1. Copy the following code into a <script> tag in an HTML file or a JS environment.
  2. Open your Browser DevTools (F12 or Right-Click > Inspect > Console).
  3. Use console.log() to check the value of orderTotal and discountAmount right before the finalPrice is calculated.
  4. Identify why the math is failing (Hint: look closely at the variable names and the types of data being used).
  5. Fix the code so that a $120 order with a 10% discount correctly outputs "Your final total is: 108" to the console.
const orderTotal = "120"; // Note the quotes
const discountPercent = 10;
const discountAmount = orderTotal * (discountPercent / 100);

// This is where it gets weird
const finalPrice = orderTotal - discountAmount;

console.log("Your final total is: " + finalPrice);
Rating
0 0

There are no comments for now.

to be the first to leave a comment.