Jul. 28, 2025
4 min read

JavaScript is a powerful but quirky language. Even experienced developers occasionally stumble over its unexpected behaviors. If you’re just starting out, you might be making some of these common mistakes without even realizing it.
Don’t worry, we’ve all been there! Here are 10 JavaScript pitfalls beginners often face and how to avoid them before they sneak into your code.
- Accidentally Creating Global Variables
function greet(){
name = 'Ruth' //Opps!, we made a global variable
}
Why it’s a problem: If you forget to use let
, const
, or var
, JavaScript assumes you meant to create a global variable. This can lead to bugs that are very hard to track down.
How to avoid it:
Always declare your variables. And consider using 'use strict';
at the top of your files to catch this early.
- Overusing
==
Instead of===
0 == false; // true
'' == 0; // true
Why it’s a problem:
==
does type coercion. Sometimes that’s handy, but often it’s just confusing.
Better practice:
Use ===
and !==
For strict comparisons, they don’t convert types behind your back.
- Using
parseInt()
Incorrectly
parseInt("08"); // returns 8
parseInt("08", 8); // returns 0 ???
What’s going on?
parseInt
can interpret the string as octal if no radix is provided.
Safer way:
Always provide the second argument:
parseInt("08", 10); // 8
- Ignoring Asynchronous Behavior
console.log("Start");
setTimeout(() => console.log("Delayed"), 0);
console.log("End");
// Output: Start → End → Delayed
Beginners often expect setTimeout
to run immediately.
Better practice: Understand the event loop. Use async/await
or .then()
for predictable async flow.
- Ignoring Error Handling in Async Code
const data = await fetchData(); // what if this fails?
Beginners skip try/catch blocks.
But async errors don’t throw like synchronous ones; they silently fail if unhandled.
Do this:
try {
const data = await fetchData();
} catch (err) {
console.error("Something went wrong", err);
}
- Reassigning Constants
const user = { name: "Alice" };
user = { name: "Bob" }; // ❌ Error
But wait!
user.name = "Bob"; // ✅ Works
Why?
const
means the binding can’t change, but the object it points to can.
Be aware:
const
keeps the variable from being reassigned, not the object from being mutated.
- Thinking
this
Works the Same Everywhere
const person = {
name: "Jane",
greet: function () {
console.log(`Hi, I’m ${this.name}`);
},
};
const greet = person.greet;
greet(); // Uh-oh: "Hi, I’m undefined"
What happened?
When you pull a method out of an object, this
gets lost.
How to fix:
Use arrow functions if you’re inside classes or bind the function properly.
- Expecting
forEach()
to Work Withasync/await
arr.forEach(async (item) => {
await doSomething(item);
});
Why it fails silently:
forEach
doesn’t wait for async
functions to finish. So your await
may not behave as expected.
What to use instead:
for (const item of arr) {
await doSomething(item);
}
Or if you’re feeling fancy, use Promise.all()
for parallel execution.
- Forgetting That
null
andundefined
Are Different
let name = null;
let age;
console.log(name); // null
console.log(age); // undefined
Beginners often assume they’re the same.
But null
is something you assign intentionally. undefined
is what JavaScript assigns when something hasn’t been defined yet.
Tip:
Use them deliberately. If you mean “nothing,” use null
. If it’s just not set yet, undefined
is fine.
- Forgetting
return
in Arrow Functions
const add = (a, b) => { a + b }; // No return = undefined
console.log(add(2, 3)); // undefined
Do this
Either use implicit return (no braces):
const add = (a, b) => a + b;
Or explicitly return
:
const add = (a, b) => { return a + b; };
Final Thoughts
Mistakes are part of learning. If you’ve made (or are making) any of these, you’re not alone. Every JavaScript dev has been there. The key is knowing what to look out for and building habits that keep your code predictable and bug-free.
Even better? Bookmark this post and come back to it every few months, just to laugh at how far you’ve come.