JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
3: Using the Browser Console and DevTools
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:
- Copy the following code into a
<script>tag in an HTML file or a JS environment. - Open your Browser DevTools (F12 or Right-Click > Inspect > Console).
- Use
console.log()to check the value oforderTotalanddiscountAmountright before thefinalPriceis calculated. - Identify why the math is failing (Hint: look closely at the variable names and the types of data being used).
- 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);
There are no comments for now.