What is undefined vs not defined in JavaScript?

The key difference between undefined and not defined is that undefined refers to a variable that has been declared but not assigned a value, while not defined refers to a variable that hasn’t been declared in the code at all. Accessing an undefined variable returns the value undefined, but trying to access a not defined variable throws a ReferenceErrorA ReferenceError occurs when you try to access a variable that hasn’t been declared or is out of scope..

Suppose you're at a restaurant and ask for dessert but don’t specify which one—this is like undefined because the request exists but has no value. If you never mention dessert at all, it’s like not defined, leading to confusion (or an error) when the waiter tries to serve something you didn’t order.

What is undefined?

In JavaScript, a variable is undefined when it has been declared but not assigned a value. This state indicates that the variable exists in memory but doesn’t hold any meaningful value yet.

Examples of undefined

  1. Declared but not assigned:

let myVar;
console.log(myVar);

Here, myVar is declared but not initialized, so JavaScript assigns it the default valueA default value is the initial value assigned to a variable if no explicit value is provided. For example, undefined is the default value for uninitialized variables. undefined.

Try assigning null to myVar instead of leaving it uninitialized. How does the output change?

  1. Missing object property:

const person = { name: "John" };
console.log(person.age); // Output: undefined

The property age does not exist in the person object, so accessing it returns undefined.

  1. Function without a return statement:

function greet() {
console.log("Hello");
}
const result = greet();
console.log(result); // Output: undefined

A function that doesn’t explicitly return a value returns undefined by default.

While undefined indicates an existing variable without a value, not defined takes it a step further to represent variables that don’t exist at all. Let’s explore this concept.

What is not defined?

In JavaScript, a variable or property is considered not defined when it hasn’t been declared at all in the current scopeScope defines where a variable can be accessed in your code, such as within a function (local scope) or globally across the entire program (global scope).. Attempting to access such a variable results in a ReferenceError.

Examples of not defined

  1. Accessing undeclared variables:

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

The variable nonExistentVar hasn’t been declared anywhere in the code, leading to an error.

  1. Misspelled variable names:

let myVariable = 10;
console.log(myvariable); // ReferenceError: myvariable is not defined

JavaScript is case-sensitive, so myVariable and myvariable are treated as different variables.

  1. Accessing properties of an undeclared object:

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

Here, the object myObject is not declared, causing an error.

Key differences between undefined and not defined

Aspect

undefined

not defined

Meaning

The variable is declared but not assigned a value.

The variable is not declared in the code.

Default Behavior

Automatically assigned by JavaScript to declared variables without a value.

JavaScript throws a ReferenceError when accessing it.

Error Type

No error occurs; value is undefined.

Throws a ReferenceError.

Example

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

console.log(y); // ReferenceError

Ready to learn JavaScript and build interactive web applications? Start our Learn JavaScript course and gain a solid foundation, preparing you for a career in web development.

Want to become a full-stack web developer? Begin our Become a Full Stack Developer skill path, starting with the basics and advancing to expert-level skills, setting you up for success in the world of full-stack development.

Key takeaways

  1. undefined: A variable or property exists but lacks a value.

  2. not defined: A variable or property does not exist in the code, resulting in an error when accessed.

  3. Always declare variables before using them to avoid ReferenceError.

  4. Avoid assigning undefined manually; let JavaScript handle it.

  5. Use tools like linters to catch undeclared variables or typos.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Is null == undefined?

Yes, null == undefined evaluates to true in JavaScript because they are loosely equal in value. However, null === undefined is false since they are of different types (null is an object, and undefined is a primitive).


What is not defined in JavaScript?

A variable is considered “not defined” in JavaScript if it has not been declared in the code. Attempting to access such a variable results in a ReferenceError.


What does "undefined" mean in JavaScript?

Undefined means a variable has been declared but not assigned a value yet. For example:

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

What is the difference between defined and undefined function?

A “defined” function exists in the code and can be executed when called. An “undefined” function is one that has not been declared or assigned, and trying to call it will result in a ReferenceError. Example:

function definedFunction() {
    console.log("I am defined.");
}
definedFunction(); // Works

undefinedFunction(); // Throws ReferenceError: undefinedFunction is not defined

Free Resources