โ ๏ธ 1. Using var Instead of let or const
The mistake:
Beginners often use var everywhere because older tutorials still show it. But var has function scope, not block scope โ which leads to weird bugs.
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 3, 3, 3
The fix:
Use let (for reassignable variables) and const (for constants).
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 0, 1, 2 โ
๐งฉ 2. Confusing == and ===
The mistake:
== performs type coercion, meaning it tries to convert values to the same type before comparing.
0 == "0"; // true
false == "false"; // false (๐คฏ)
The fix:
Always use === (strict equality) to avoid weird surprises.
0 === "0"; // false โ
๐ณ๏ธ 3. Not Understanding this
The mistake:
Beginners often think this refers to the current function โ but it actually depends on how the function is called.
const obj = {
name: "JS",
getName: function() {
return this.name;
},
};
const fn = obj.getName;
console.log(fn()); // undefined ๐ต
The fix:
Use arrow functions or .bind() to control this.
const fn = obj.getName.bind(obj);
console.log(fn()); // "JS" โ
Or use an arrow function in classes or callbacks, where it preserves the parent scope.
๐ 4. Forgetting About Asynchronous Code
The mistake:
Assuming code runs line by line synchronously.
console.log("A");
setTimeout(() => console.log("B"), 0);
console.log("C");
// Output: A, C, B ๐คฏ
The fix:
Learn Promises, async/await, and the event loop.
async function demo() {
console.log("A");
await Promise.resolve();
console.log("B");
console.log("C");
}
demo();
// Output: A, B, C โ
๐งฎ 5. Not Declaring Variables Properly
The mistake:
Using undeclared variables creates global ones unintentionally.
x = 10; // No 'let' or 'const'!
console.log(window.x); // 10 ๐ฑ
The fix:
Always declare variables with let or const.
Use "use strict" at the top of your file to prevent this mistake.
"use strict";
let x = 10; // โ
๐งฐ 6. Not Understanding How Objects and Arrays Work by Reference
The mistake:
Beginners think copying an array or object makes a new one โ it doesnโt!
const arr1 = [1, 2, 3];
const arr2 = arr1;
arr2.push(4);
console.log(arr1); // [1, 2, 3, 4] ๐ฌ
The fix:
Use the spread operator or structured cloning.
const arr2 = [...arr1]; // โ
arr2.push(4);
console.log(arr1); // [1, 2, 3]
๐ 7. Ignoring the Browser Console
The mistake:
Many beginners avoid the console because errors look scary.
The fix:
The console is your best friend!
Check errors, use console.log(), and experiment.
console.log("Debugging is fun!");
console.error("Something went wrong!");
console.table([{ name: "JS", age: 25 }, { name: "HTML", age: 30 }]);
๐ Final Words
Learning JavaScript isnโt about memorizing syntax โ itโs about understanding how things work under the hood.
If you master these 7 mistakes, youโll already be ahead of most beginners.
Keep experimenting, break things, fix them โ and soon, JavaScript will start making sense.
๐ฌ What about you?
Which JavaScript mistake took you the longest to fix?
Share your experience in the comments โ letโs learn together!
Top comments (0)