By the end of this lesson, you will be able to recognize and fix the JavaScript mistakes that most often cause silent bugs: loose equality, undeclared variables, and unexpected scope.
What it is
Common beginner mistakes in JavaScript are small coding habits that produce surprising behavior. The most frequent ones involve == instead of ===, assigning to variables that were never declared, and using var when let or const would be safer. These mistakes are dangerous because JavaScript often continues running instead of stopping with a clear error.
A useful mental model is: JavaScript is forgiving about types and declarations, but forgiving does not mean correct. Related terms include type coercion, global scope, strict mode, and block scope.
Why it matters
- Using
===prevents false matches such as0 == "0". - Declaring variables with
letorconstavoids accidental global variables that can overwrite other code. - Strict mode turns some silent mistakes into clear errors, making debugging faster.
- Block-scoped variables reduce bugs in loops and functions.
- Safe copying habits prevent objects and arrays from being changed unexpectedly.
Syntax or steps
Use this smallest safe pattern:
- Start scripts with
"use strict";when working in plain JavaScript files or console experiments. - Declare every variable with
constunless it must be reassigned, then uselet. - Compare values with
===and!==. - Use
letfor loop counters that need block scope.
Example
"use strict";
function addTax(price) {
tax = 0.2; // ReferenceError: tax is not defined
return price + tax;
}
try {
addTax(10);
} catch (error) {
console.log(error.message);
}
function addTaxFixed(price) {
const tax = 0.2;
return price + tax;
}
console.log(addTaxFixed(10));
console.log(0 == "0");
console.log(0 === "0");
Part by part:
"use strict";makes undeclared assignment fail loudly instead of creating a global variable.tax = 0.2;is the mistake:taxwas never declared.tryandcatchshow the error message without stopping the rest of the example.const tax = 0.2;is the fix: the variable is declared and scoped to the function.0 == "0"printstruebecause JavaScript converts the string to a number.0 === "0"printsfalsebecause the types are different.
Common mistakes
- Using
==by habit. Fix: default to===. If you need to check for missing values, usevalue === null || value === undefined. - Forgetting to declare variables. Fix: use
constorletand enable strict mode. - Using
varin loops. Fix: useletso each loop iteration gets its own binding. - Assuming assignment copies objects. Fix: use
slice()for arrays or spread syntax for shallow copies, and remember nested objects still share references.
When to use it
| Situation | Risky choice | Safer choice | When the risky choice may be acceptable |
|---|---|---|---|
| Comparing values | == | === | Rarely, only when you intentionally want coercion. |
| Declaring variables | no declaration | const or let | Almost never in new code. |
| Loop counters | var | let | Legacy code where function scope is understood. |
Practice
Guided exercise: rewrite this code so it does not create a global variable and uses strict equality.
count = 5;
if (count == "5") {
console.log("match");
}
Expected fixed output: match should not print, and count should be declared locally.
Challenge: write a function named isSameUser that returns true only when both arguments are the same type and value. For example, isSameUser(5, "5") should return false. Hint: use ===.
Quick check
Question: Why is 0 === "0" safer than 0 == "0" in most JavaScript code?
Answer: === checks both value and type, so it does not silently convert the string "0" into the number 0.
Summary
Most beginner JavaScript bugs come from silent coercion and accidental scope. Use ===, declare variables with const or let, and prefer strict mode so mistakes become visible errors.