DEV Community

hmza
hmza

Posted on

⚠️ Common JavaScript Issues Every Developer Faces (And How to Fix Them)

⚠️ 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  

Enter fullscreen mode Exit fullscreen mode
  • 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  

Enter fullscreen mode Exit fullscreen mode
  • == 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);
  }
};

Enter fullscreen mode Exit fullscreen mode

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;
}

Enter fullscreen mode Exit fullscreen mode

🛠 Fix: Stick with async/await for readability. Don’t mix with .then() unless necessary.


5. 📦 Undeclared Variables


x = 10; // global leak 😡  

Enter fullscreen mode Exit fullscreen mode

🛠 Fix: Use let, const, or var to declare variables.


let x = 10;

Enter fullscreen mode Exit fullscreen mode

6. 🔁 Loop Scope Issues


for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}
// Outputs: 3, 3, 3

Enter fullscreen mode Exit fullscreen mode

🛠 Fix: Use let:


for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}
// Outputs: 0, 1, 2

Enter fullscreen mode Exit fullscreen mode

7. 🧼 No Error Handling in Promises


fetch("/api").then(res => res.json()).then(data => console.log(data));

Enter fullscreen mode Exit fullscreen mode

🛠 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);
  }
}

Enter fullscreen mode Exit fullscreen mode

✅ Final Advice

  • Use a linter like ESLint
  • Write unit tests
  • Use "use strict"; mode
  • TypeScript is your friend 👀

Happy hacking, JavaScript warrior 🛡️💥


Javascript Sucks

Top comments (0)