Difference Between undeclared, undefined, and null

Madhusudan DasMadhusudan Das
1 min read
  1. Undeclared Variable:

    • An undeclared variable is one that has not been declared in the current scope.

    • Attempting to access an undeclared variable will result in a ReferenceError.

Example:

    console.log(brain); // ReferenceError: brain is not defined
  1. Undefined Variable:

    • An undefined variable is one that has been declared but has not been assigned a value.

    • JavaScript initializes declared variables with undefined by default.

Example:

    let heart;
    console.log(heart); // Outputs: undefined
  1. Null Variable:

    • A null variable is one that has been explicitly assigned the value null.

    • It represents the intentional absence of any object value.

Example:

    let soul = null;
    console.log(soul); // Outputs: null

In summary:

  • "Undeclared" means the variable has not been declared or defined in the current scope.

  • "Undefined" means the variable has been declared but not assigned a value, or it can be the default value of uninitialized variables.

  • "Null" means the variable has been intentionally set to a value representing the absence of any object.

0
Subscribe to my newsletter

Read articles from Madhusudan Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Madhusudan Das
Madhusudan Das

๐Ÿ‘‹ I'm a Jamshedpur native with a deep passion for entrepreneurship and web development. Armed with expertise in HTML, CSS, JavaScript, Node.js, and React, I specialize in crafting high-performance web applications that seamlessly blend design and technology. ๐Ÿš€ One of my most significant accomplishments includes co-founding a startup and managing it for a successful three-year run. In just nine months, I took the product from inception to a thriving ecosystem with 25+ merchants and a loyal customer base of 1500+. ๐ŸŒŸ I have over five years of frontend development experience and a passion for pushing the boundaries of web design with React.js. Beyond coding, you'll find me engaged in โ™Ÿ๏ธ chess battles, ๐ŸŽฌ movies, and grooving ๐ŸŽต to music with friends. I'm a detail-oriented computer science engineer committed to excellence in every project. Let's create something extraordinary together!