Skip to content

Latest commit

 

History

History
111 lines (89 loc) · 4 KB

File metadata and controls

111 lines (89 loc) · 4 KB

What is the difference between undefined and not defined in JavaScript ?

In JavaScript, undefined and not defined are terms often encountered when dealing with variables. While they may seem similar, they have distinct meanings and occur under different circumstances.


1. undefined

  • Definition: A variable is assigned the value undefined when it is declared but not initialized or when a function does not return a value explicitly.
  • Type: It is a built-in primitive type in JavaScript.
  • Scope: Variables that are declared but not assigned any value will automatically have the undefined value.

Examples of undefined:

  1. Uninitialized Variable:

    let x;
    console.log(x); // Output: undefined

    Here, x is declared but not assigned any value, so its value is undefined.

  2. Accessing a Non-Existent Property:

    const obj = {};
    console.log(obj.name); // Output: undefined

    The property name does not exist in obj, so the output is undefined.

  3. Functions Without a Return Statement:

    function example() {}
    console.log(example()); // Output: undefined

    If a function does not explicitly return a value, it implicitly returns undefined.


2. not defined

  • Definition: A variable is considered "not defined" when it has not been declared in the current scope.
  • Error: Accessing a variable that is not declared will result in a ReferenceError.

Examples of not defined:

  1. Using an Undeclared Variable:

    console.log(y); // ReferenceError: y is not defined

    Here, y was never declared, so JavaScript throws an error.

  2. Accessing Out of Scope:

    {
      let z = 10;
    }
    console.log(z); // ReferenceError: z is not defined

    The variable z is block-scoped and cannot be accessed outside the block.


Key Differences Between undefined and not defined

Feature undefined not defined
Occurs When A variable is declared but not initialized. A variable is accessed without declaration.
Type A primitive type in JavaScript. A ReferenceError is thrown.
Scope Variable exists but lacks a value. Variable does not exist in any scope.
Example Output undefined ReferenceError

Real-World Scenario

Example of undefined:

function getValue(obj, key) {
  return obj[key]; // May return undefined if the key does not exist
}

const user = { name: "Alice" };
console.log(getValue(user, "age")); // Output: undefined

Example of not defined:

try {
  console.log(score); // Throws ReferenceError
} catch (error) {
  console.log("Error:", error.message); // Output: Error: score is not defined
}

How to Avoid Confusion

  1. Declare All Variables: Always declare variables using let, const, or var to prevent not defined errors.
  2. Use typeof Safely:
    • typeof does not throw an error for not defined variables, but returns "undefined" instead:
      console.log(typeof unknownVar); // Output: "undefined"
  3. Check for Property Existence:
    • Use hasOwnProperty or optional chaining to verify a property’s existence:
      const obj = {};
      console.log(obj?.key); // Output: undefined

Conclusion

  • undefined signifies a declared variable without a value.
  • not defined occurs when attempting to access an undeclared variable. Understanding the difference helps in debugging code and handling variables more effectively in JavaScript.