You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript allocates memory to variables and functions before executing any code.
Even before a line of code is run, memory space is reserved for variables.
undefined is a primitive value automatically assigned to variables that have been declared but have not been initialized with a value.
The value of a variable that hasn't been assigned is undefined.
undefined acts as a placeholder or default value in memory until a variable is assigned a different value.
Difference between undefined and not defined :
undefined means that memory has been allocated to a variable but no value has been assigned yet.
not defined refers to a variable that has not been declared or allocated any memory.
undefined vs Empty :
undefined is not equivalent to empty or null.
It is a special keyword in JavaScript that takes up its own memory space.
undefined is a placeholder until a value is assigned to a variable.
Examples of undefined :
// Example 1vara;// Memory is allocated for 'a', but no value is assigned yetconsole.log(a);// Output: undefined// Example 2varx;console.log(x);// Output: undefined// It represents the absence of a value.letarr=[1,2,,4]console.log(arr[2])// undefined because of empty array at index 2// not defined :-**notdefined**meanswehaven't yet defined anything.
-**notdefined**typicallyreferstoasituationwhereavariableoridentifierhasnotbeendeclaredorisoutofscope.// Example 3console.log(y);// Output: ReferenceError: y is not defined
JS is a loosely typed / weakly typed language.
It doesn't attach variables to any datatype.
We can say var b = 100, and then change the value to boolean a = true or string a = 'hello world' later on.
vara;console.log(a);// undefineda=100;console.log(a);// 100a="hello world";console.log(a);// hello world
Never assign undefined to a variable manually. Let it happen on it's own accord.