Differences Between Undeclared and Undefined Variables in JavaScript
Last Updated :
13 Dec, 2024
In JavaScript, variables are declared using keywords like var, let, or const. The scope and behavior of variables depend on how they are declared:
- var: Has function scope or global scope. If not declared explicitly, a var variable becomes an undeclared global variable when assigned a value.
- let and const: Have block scope. They throw a ReferenceError if used before declaration or when undeclared.
An undeclared variable is created implicitly by assigning a value without declaring it. An undefined variable is one that is declared but not assigned a value.
JavaScript
// Undefined variable
// Declared but not assigned
let a;
// Output: undefined
console.log(a);
// Undeclared variable
console.log(b);
the variable a is declared but not initialized hence it will give the undefined as a value. while the b will show the error as it is not declared anywhere in the code.
1. Undefined Variable
The variables which are written in the code but haven't been assigned any value yet are called undefined.
Syntax:
let x;
console.log(x);
For instance, in this case, the variable x will remain undefined till the assignment.
- In the memory Allocation phase: JavaScript will scan all the variables and functions in the code. Here, we have only variable x in the entire code. So, it will get space in memory, and undefined will be assigned to each variable(only x in our case).
- In the code execution phase: In the first line of code, console.log(x) is written. Now JavaScript will search for this variable in memory and it will be found, but as the JavaScript engine hasn't encountered any line on which the programmer is assigning some value to x. Hence undefined will be printed. Later, in the next line, (x=5), the undefined will be replaced by 5.
JavaScript
let x;
console.log(x);
// Now, variable is no longer undefined
x = 5;
console.log(x);
2. Undeclared Variable
The variables which don't exist in the memory heap area, ie., not written inside the code, are called undeclared.
Syntax:
console.log(y); // y will be considered as undeclared
For instance, in this case, the variable y is not declared in the code ie., in the memory allocation phase, no space was allocated which has been named y. So it will be considered as undeclared.
JavaScript
let x;
// y will be undeclared
console.log(y);
x = x * 2;
- In the memory allocation phase: All variables and functions (only x in our case) will get space in memory.
- In the thread of execution: JavaScript will encounter the first line of console.log(y). Now, it will search y in memory space but y will not be found. Hence, it will be considered undeclared.
Output:

Difference Between Undeclared and Undefined Variables
Undeclared | Undefined |
---|
These are the variables that do not exist in the memory heap. | These variables are the ones that do exist in memory, but nothing is being assigned to them explicitly by the programmer. |
The variables are considered to be undeclared because of programmer does not write them with var, let, or const. | The variables are considered to be undefined because it is assigned by JavaScript to them. |
If we try to access them in the code execution phase, then JavaScript will throw a Reference error. | If we try to access these variables, we'll get the undefined as value. |
Practical Illustration:
In the memory allocation phase, the variable a will get memory, and JavaScript will assign undefined to it. You can see the situation in the first breakpoint where we have stopped our program at the first line of code.
After then, the code execution phase will start with the first line of the console.log, undefined will be printed. Later the value of a will be replaced by 5, you can observe this on the second breakpoint. And finally, when the code will try to access some kind of variable(b in our case) that doesn't exist in memory it will throw a Reference error because it was undeclared by the programmer.
JavaScript
// a will be undefined here
console.log(a);
var a = 5;
// a will be 5 here
console.log(a);
// b will be undeclared here
console.log(b);
Output:
Similar Reads
What are undeclared and undefined variables in JavaScript? Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of
1 min read
Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It
2 min read
Difference between var and let in JavaScript In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable o
3 min read
Difference between var, let and const keywords in JavaScript JavaScript provides three ways to declare variables: var, let, and const, but they differ in scope, hoisting behaviour, and re-assignment rules. Understanding these differences helps write more predictable and maintainable code.var: Declares variables with function or global scope and allows re-decl
6 min read
Explain the difference between undefined and not defined in JavaScript In JavaScript, they both are related to memory space and there is a very simple difference between them. If the variable name which is being accessed doesn't exist in memory space then it would be not defined, and if exists in memory space but hasn't been assigned any value till now, then it would b
3 min read