Hey, are you a beginner in JavaScript?
And your code breaks a lot? It gives error but you don’t know what went wrong?
It’s okay. It happens to all of us.
In this post, I’ll show you how to catch and fix bugs in very easy way using small tools and tricks.
No difficult words. Just real steps and copy-paste code you can use in your own project.
🔧 1. Use console.log()
– but smartly
Yes, console.log()
is your best friend.
But you need to use it in correct way so you understand what’s really happening.
✅ Good Example
let username = "meena";
console.log("Username is:", username);
Or even cleaner:
console.log({ username });
That gives you output like:
{ username: "meena" }
You know now what you’re looking at.
❌ Bad Example
console.log(username);
Now if you have 5 variables, you’ll get confused:
Which one is which?
🎨 Add Color to Logs
You can also use color to highlight logs:
console.log("%cDebug: App Loaded", "color: blue; font-size: 14px;");
Try using red for errors, green for success, yellow for warnings.
🧠 Use When Needed Only
Don’t spam log:
for (let i = 0; i < 10000; i++) {
console.log(i); // ❌ Too much
}
Do this instead:
if (i % 500 === 0) console.log("i is", i);
🧰 2. Pause Code Using Browser – Watch it Like a Movie
Your browser can pause your code at any line.
You can then see what’s inside each variable at that moment.
🖥 Steps (works in Chrome):
- Press
Ctrl + Shift + I
→ DevTools open - Go to
Sources
tab - Click on your JS file
- Click on line number – this sets breakpoint
- Refresh page
- Code stops there – now hover mouse on any variable
You will see values, one by one.
✅ Add Condition
Right-click on a line → choose “Add conditional breakpoint”
Type:
score > 100
Now it pauses only when score is more than 100.
Super helpful!
🧠 3. Read Error Message (Stack Trace) – It Shows You the Place
When your code crashes, look at the red error in console.
It will show something like:
TypeError: Cannot read property 'name' of undefined
at script.js:20:5
What it means:
- Line 20: Error happened here
-
‘name’ of undefined: You tried to use
.name
on something that isundefined
✅ Fix It Like This
console.log("user is:", user);
console.log("user.name is:", user?.name); // optional chaining
Or:
if (!user) {
console.error("User is missing");
}
Bonus Tool: Assertion
console.assert(user !== undefined, "User is undefined");
It gives message only when condition fails.
🪛 4. Break Your Big Function into Small Pieces
If your function is too big, it’s hard to find where bug is hiding.
Split it into 2–3 small steps.
👇 Try Like This
function processOrder(order) {
console.log("Order received:", order);
let checked = checkOrder(order);
console.log("Checked:", checked);
if (!checked) return;
let billed = createBill(order);
console.log("Billed:", billed);
}
If something breaks, you’ll know which step is broken.
That’s called step-by-step debugging.
🔍 5. Always Check Input – Before You Use It
Beginners often forget to check if value is correct before using.
Like, this will crash:
console.log(user.name); // ❌ if user is null
✅ Safe Way:
if (user && typeof user === "object") {
console.log("User name:", user.name);
}
Or shorter:
console.log("User name:", user?.name);
🧪 Extra Tools
typeof age === "number"; // Is it number?
Array.isArray(fruits); // Is it array?
typeof greet === "function"; // Is it function?
These checks can save hours of head scratching.
📄 Copy-Paste Debug Checklist
✅ Task | 💡 How To Do It |
---|---|
Check variable value | console.log("age:", age) |
Pause code at a line | Use DevTools → Sources → Click Line |
Stop only if value wrong | Right-click → Conditional Breakpoint |
Prevent crash on .name
|
Use user?.name or check with if
|
Is it array? | Array.isArray(things) |
Add colors to console logs |
%c message , "color: red"
|
Use console.assert()
|
console.assert(user, "Missing user") |
🧪 Online Tools to Practice
Try your code, logs, and breakpoints here:
🔹 jsfiddle.net
🔹 codesandbox.io
🔹 stackblitz.com
You don’t need to install anything.
💬 Final Words (speak like a human)
Bugs will happen. That’s okay.
But now you know how to look at the bug, follow the signs, and catch it using logs, DevTools, and small tricks.
Use small logs. Watch values. Check inputs. Pause code.
These are your daily tools. Even expert coders use them.
If you found this post helpful, give it a 💖 on Dev.to, and share your debugging trick in comments.
Happy bug hunting 🐞!
Top comments (0)