undefined value in JS
In Javascript, undefined
does not mean some empty value or undeclared variable.
If a variable has a value undefined
, it means it has been declared in the code, it has been hoisted in the memory space/execution context but has not been assigned any value. Thus undefined
is a special value in javascript, all variables declared in the code are assigned this undefined value in the 1st Hoisting phase of code execution cycle. ( Javascript code is executed in two phases - see Javascript Code Execution topic).
If your code uses some undeclared value, the javascript execution will throw an error 'Uncaught Reference Error: <<var>> is not declared'
Since undefined
is a special variable/value in javascript, we can write boolean expressions comparing a variable to undefined
.
When an undeclared variable is assigned a value, the JS engine (in non-strict mode) would automatically add the variable to the global object in the 1st Hoisting phase of code execution.
Last updated