⚠️ Common JavaScript Issues Every Developer Faces (And How to Fix Them)
JavaScript is awesome — but sometimes it feels like it’s actively trying to break your brain. Here's a list of common issues (and how to survive them).
1. ❌ undefined
vs null
let x; // undefined
let y = null; // explicitly null
-
undefined
means a variable was declared but not assigned. -
null
means you deliberately emptied it.
🛠 Fix: Know when to use each and initialize your variables properly.
2. 🧮 Loose vs Strict Equality
0 == false // true
0 === false // false
-
==
does type coercion. -
===
checks both value and type.
🛠 Fix: Always prefer ===
and !==
.
3. 🌀 The this
Keyword Trap
const person = {
name: "Yuri",
greet() {
console.log("Hi, I'm " + this.name);
}
};
Arrow functions don’t bind this
the way you expect in object methods.
🛠 Fix: Use traditional functions in objects or .bind(this)
if needed.
4. 🔄 Async/Await Abuse
async function fetchData() {
const res = await fetch("/api/data");
const data = await res.json();
return data;
}
🛠 Fix: Stick with async/await
for readability. Don’t mix with .then()
unless necessary.
5. 📦 Undeclared Variables
x = 10; // global leak 😡
🛠 Fix: Use let
, const
, or var
to declare variables.
let x = 10;
6. 🔁 Loop Scope Issues
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Outputs: 3, 3, 3
🛠 Fix: Use let
:
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Outputs: 0, 1, 2
7. 🧼 No Error Handling in Promises
fetch("/api").then(res => res.json()).then(data => console.log(data));
🛠 Fix: Use .catch()
or a try/catch block with async/await
:
async function safeFetch() {
try {
const res = await fetch("/api");
const data = await res.json();
console.log(data);
} catch (error) {
console.error("Something went wrong!", error);
}
}
✅ Final Advice
- Use a linter like ESLint
- Write unit tests
- Use
"use strict";
mode - TypeScript is your friend 👀
Happy hacking, JavaScript warrior 🛡️💥
Top comments (0)