Back to JavaScript Notes
Topic #372

Common Mistakes

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 as 0 == "0".
  • Declaring variables with let or const avoids 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:

  1. Start scripts with "use strict"; when working in plain JavaScript files or console experiments.
  2. Declare every variable with const unless it must be reassigned, then use let.
  3. Compare values with === and !==.
  4. Use let for 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: tax was never declared.
  • try and catch show 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" prints true because JavaScript converts the string to a number.
  • 0 === "0" prints false because the types are different.

Common mistakes

  • Using == by habit. Fix: default to ===. If you need to check for missing values, use value === null || value === undefined.
  • Forgetting to declare variables. Fix: use const or let and enable strict mode.
  • Using var in loops. Fix: use let so 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

SituationRisky choiceSafer choiceWhen the risky choice may be acceptable
Comparing values=====Rarely, only when you intentionally want coercion.
Declaring variablesno declarationconst or letAlmost never in new code.
Loop countersvarletLegacy 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Common Mistakes – FAQs

Quick answers about learning Common Mistakes in JavaScript.

This free note from Coding Now Tech Institute explains Common Mistakes in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Common Mistakes, is 100% free with no signup required.
With focused practice, most students grasp Common Mistakes in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now