First, we need to understand the JavaScript Code Execution process, which consists of two phases: the Memory Creation Phase and the Code Execution Phase. Both 'undefined' and 'not defined' are related to memory space.
undefined
In the Memory Creation Phase, variables and functions are stored as key-value pairs. JavaScript assigns undefined to each variable as a placeholder. This temporary value stays until JavaScript finds the real value, then it replaces undefined with the real one. Don't assign undefined to any variables; it's not a good practice.
var x ;
console.log(x);// undefined
x = 2;
console.log(x); //2
not defined
In JavaScript, if a variable is not declared at all and we try to access it, it will throw a ReferenceError.
console.log(x);//ReferenceError: x is not defined
credits to Akshay Saini https://youtu.be/B7iF6G3EyIk?si=0WQLx-yjVOgdkkIn
Top comments (0)