Undefined vs not Defined in JavaScript

In this tutorial we will discuss about undefined and not defined, in Javascript undefined refers to the absence of a value and the not defined refers to the absense of a variable, Understanding the diffference betnween undefined and not defined is very important for debugging and writing clear code logic.

Undefined:

A variable in Javascript output as undefined when it has been declared but has not been assigned a value means varibale does not have any value. The undefined keyword is used by JavaScript to indicate that the variable exists, but there is no value has been set to the variable.

Features of Undefined:

  • JavaScript assigns undefined value by default to any declared variable that has no value.
  • We can also assign the value undefined explicitly.
  • In the memory the variable exists.
Example of Undefined:
        
        //EX 01: Variable Declaration Without Initialization
let name;
console.log(name); // Output: undefined

//Ex 02: Accessing Non-Existent Object Properties
let user = { name: 'John' };
console.log(user.age); // Output: undefined

//Ex 03: Function Parameters Without Arguments
function greet(name) {
    console.log("Hello " + name);
}
greet(); // Output: Hello undefined


//Ex 04: Functions Without a Return Statement
function noReturn() {
    // No return statement
}
let result = noReturn();
console.log(result); // Output: undefined

//Ex 05: Array Elements That Don't Exist
let arr = [1, 2, 3];
console.log(arr[5]); // Output: undefined

//Ex 06: Accessing Deleted Array Elements
let arr = [1, 2, 3];
delete arr[1];
console.log(arr[1]); // Output: undefined

//Ex 07: Using void Operator
let x = void 0;
console.log(x); // Output: undefined

//Ex 08: Using typeof on an Uninitialized Variable
let y;
console.log(typeof y); // Output: "undefined"

//Ex 09: Explicitly Assigning undefined
let z = undefined;
console.log(z); // Output: undefined

//Ex 10: Accessing a Non-Existent Return Value from a Function
function example() {}
let returnValue = example();
console.log(returnValue); // Output: undefined
        
    

Not Defined:

A variable in Javascript output as not defined ReferenceError when it has not been declared and used.

in other word, not defined occurs when a variable is referenced in the code that has never been declared in any scope. This results in a ReferenceError.

Features of Not Defined:

  • In memory the variable does not exist.
  • JS throws a ReferenceError if you attempt to access a variable that has newver been declared in any scope.

Example of Not Defined:

        
        //Ex 01:  Accessing a Variable That Hasn't Been Declared
console.log(age); //Output as ReferenceError: age is not defined

//Ex 02:  Misspelled Variable Names
let y = 5;
console.log(Y); // ReferenceError: Y is not defined

//Ex 03:  Using a Variable Before Declaration (Without let, const, or var)
console.log(z); // ReferenceError: z is not defined
let z = 10;

//Ex 04:  Variables in Block Scope (Using let or const)
if (true) {
    let blockVar = "I'm inside a block";
}
console.log(blockVar); // ReferenceError: blockVar is not defined

//Ex 05:  Accessing Variables Inside a Function Scope
function test() {
    let localVar = "I'm inside a function";
}
console.log(localVar); // ReferenceError: localVar is not defined

//Ex 06:  Accessing Global Variables in Strict Mode
"use strict";
someGlobalVar = 10; // ReferenceError: someGlobalVar is not defined
        
    

Related Post

Diffrences Between Undefined and Not Defined:

AspectUndefinedNot Defined
Variable StatusDeclared but no value assignedNot declared at all
Error TypeNo error, but variable has the value undefinedReferenceError is thrown
Examplelet x; console.log(x);undefinedconsole.log(x);ReferenceError: x is not defined

Leave a comment