Explain the difference between undefined and not defined in JavaScript
Last Updated :
18 Aug, 2021
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 be undefined.
undefined: It is a JavaScript keyword that has a special meaning. Everything which gets a space in memory will contain undefined until we assign a value to that memory space.
Let's understand how the JavaScript code is being executed to see a more clear picture. Everything in JavaScript happens inside the execution context. Execution context is the little separate section where code is being executed and variables get their memory space.
The JavaScript code is being executed in two-phase,
- The first one is the memory allocation phase during this all the variables and function definitions get stored inside the memory heap. The JavaScript assigns undefined to each variable in this phase.
- The second one is a thread of the execution phase, during this the code written inside the JavaScript file is being executed.
Each variable holds the value undefined till the program reaches the line where we have assigned that variable. After that line, the variable's undefined value gets replaced by the original value.
Example 1: The global execution context will be created and in the memory allocation phase, the var a will get space in memory, and JavaScript will assign undefined to it. During the thread of execution, the JavaScript will encounter the first line console.log(a) and as we haven't assigned the value for a, undefined will be printed on the console. In the next line, we have assigned 5 to a, hence the variable a is no more undefined. Now it contains the value 5. So next time whenever we access the variable a, it won't be evaluated as undefined. So it will print the actual value of a.
HTML
<script>
console.log(a);
var a = 5;
console.log(a);
</script>
Output (In console):
undefined
5
Additional Points:
- If you are assigning a function call to a variable, and that function doesn't return anything, the variable will become undefined.
- You can explicitly assign undefined to any variable but it is not good practice to use language keywords in the way it is not expected.
not defined: In JavaScript, it is one of the reference errors that JavaScript will throw when someone accesses the variable which is not inside the memory heap.
Example 2: First of all, global execution context will be created and in the memory allocation phase, the variable "a" will get space in memory, and by default, JavaScript assigns undefined to "a". During the thread of execution, the "console.log(a)" will be printed as undefined. In the next line, we have assigned 5 to variable a. In the console, 5 will be printed. At the last line when JavaScript encounters the "console.log(b)" it searches for "b" inside the memory heap of execution context but it is not available, the JS engine will throw the "Reference Error" with a message of "b is not defined". The JavaScript will stop execution when it encounters a reference error.
HTML
<script>
console.log(a);
var a = 5;
console.log(a);
console.log(b);
</script>
Output:
Difference between undefined and not defined
undefined | not defined |
---|
It works like when we declared a variable in the code but did not assign the value before printing the variable value | It works like when we did not declare the variable and try to call that variable. |
Similar Reads
Differences Between Undeclared and Undefined Variables in JavaScript 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 co
4 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 Scope and Closures in JavaScript The scope and closures are fundamental concepts that play a crucial role in how variables are accessed and managed within the functions and blocks of code. In this article, we will learn about the difference between scope and closures in JavaScript. What is Scope?The Scope in JavaScript refers to th
2 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
Difference between Anonymous and Named functions in JavaScript In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k
4 min read
Difference between function expression vs declaration in JavaScript Function Declaration: A Function Declaration( or a Function Statement) defines a function with the specified parameters without requiring a variable assignment. They exist on their own, i.e, they are standalone constructs and cannot be nested within a non-function block. A function is declared using
1 min read