Common Errors

  • This lesson covers common JavaScript errors and how to resolve them.
  • Introduction to JavaScript Errors

    In JavaScript, an error is a problem that occurs during the execution of a program and stops or disrupts normal flow.

    Errors are common, especially for beginners, and understanding them is essential to:

    • Debug code faster

    • Write stable applications

    • Perform well in interviews

    • Build professional-quality software

    What Is a JavaScript Error ?

    A JavaScript error is an object that provides information about:

    • What went wrong

    • Where it went wrong

    • Why the code failed

    Errors usually appear in the browser console.

    Why Do Errors Occur ?

    Errors can occur due to:

    • Wrong syntax

    • Logical mistakes

    • Undefined variables

    • Invalid operations

    • Incorrect use of functions or methods

    Types of Common JavaScript Errors

    JavaScript errors are broadly categorized into:

    1. Syntax Errors

    2. Reference Errors

    3. Type Errors

    4. Range Errors

    5. Logical Errors (not thrown by JS, but very common)

    Syntax Error

    What Is a Syntax Error ?

    A Syntax Error occurs when JavaScript code does not follow correct language rules.

    These errors are detected before execution.

    Example of Syntax Error

    if (x > 10 {

      console.log("Greater");

    }

    Error Reason

    Missing closing parenthesis )

Syntax Error (Missing Parenthesis)

Missing closing parenthesis causes a syntax error.

let a = 10
let b = 20
console.log(a + b
  • Missing closing parenthesis causes a syntax error.

    How to Fix Syntax Errors

    • Check brackets ( ) { } [ ]

    • Check quotes " " ' '

    • Use proper semicolons

    • Use code editor with syntax highlighting

    Reference Error

    What Is a Reference Error ?

    A Reference Error occurs when you try to use a variable that does not exist or is out of scope.

    Example of Reference Error

    console.log(age);

    Error Reason

    • age is not defined

Scope Error Example

Variable defined inside a function cannot be accessed outside.

function test() {
  let x = 10;
}

console.log(x);
  • x is block-scoped and not accessible outside the function.

    How to Fix Reference Errors

    • Declare variables using let, const, or var

    • Check variable scope

    • Avoid typos in variable names

    Type Error

    What Is a Type Error ?

    A Type Error occurs when:

    • A value is used in an invalid way

    • A method is called on the wrong data type

Type Error Example

Calling a string method on a number causes a type error.

let num = 10;
num.toUpperCase();
  • Error Reason

    • toUpperCase() works on strings, not numbers

Null Reference Error Example

Accessing a property on null causes a runtime error.

let user = null;
console.log(user.name);
  • Trying to access property of null.

    How to Fix Type Errors

    • Check data types before using methods

    • Validate variables

    • Use optional chaining when needed

    Example:

    console.log(user?.name);

    Range Error

    What Is a Range Error ?

    A Range Error occurs when a value is outside the allowed range.

    Example of Range Error

    let arr = new Array(-5);

    Array length cannot be negative.

Infinite Recursion Error Example

Function calls itself endlessly, causing a stack overflow error.

function recurse() {
  recurse();
}

recurse();
  • This causes a Maximum call stack size exceeded error.

    How to Fix Range Errors

    • Check limits and boundaries

    • Avoid infinite recursion

    • Validate input values

    Logical Errors

    What Is a Logical Error ?

    A Logical Error happens when:

    • Code runs without errors

    • Output is incorrect

    JavaScript does not throw an error for logical mistakes.

Conditional Statement Example

Uses if-else to check marks and display pass or fail result.

let marks = 35;

if (marks > 40) {
  console.log("Pass");
} else {
  console.log("Fail");
}
  • If pass marks are >= 35, logic is incorrect.

    Why Logical Errors Are Dangerous

    • No error message

    • Hard to detect

    • Incorrect application behavior


    How to Fix Logical Errors

    • Test edge cases

    • Use console.log() for debugging

    • Write proper conditions

    • Add comments for clarity

    Runtime Errors vs Compile-Time Errors

    Error Type

    When It Occurs

    Syntax Error

    Before execution

    Reference Error

    During execution

    Type Error

    During execution

    Range Error

    During execution

    Logical Error

    During execution (no error thrown)

    How to See Errors in JavaScript

    • Browser Console (Chrome DevTools)

    • Node.js terminal

    • Error messages with line numbers

    Reading an Error Message (Important Skill)

    Example:

    Uncaught ReferenceError: x is not defined

        at script.js:5

    Meaning:

    • Error type: ReferenceError

    • Problem: x is not defined

    • Location: Line 5

    Common Mistakes That Cause Errors

    • Forgetting variable declarations

    • Using == instead of ===

    • Calling methods on wrong data types

    • Misspelling variable or function names

    • Forgetting return statements

    • Ignoring console errors

    Best Practices to Avoid Common Errors

    • Always declare variables

    • Use let and const

    • Write clean and readable code

    • Use strict equality (===)

    • Test code frequently

    • Read error messages carefully

    Real-World Importance of Error Understanding

    • Faster debugging

    • Cleaner code

    • Better application stability

    • Higher interview confidence

    • Professional development skills