FrontendDeveloper.in

JavaScript question detail

What are the differences between undeclared and undefined variables

Below are the major differences between undeclared(not defined) and undefined variables,

undeclaredundefined
These variables do not exist in a program and are not declaredThese variables declared in the program but have not assigned any value
If you try to read the value of an undeclared variable, then a runtime error is encounteredIf you try to read the value of an undefined variable, an undefined value is returned.
var a;
a; // yields undefined

b; // Throws runtime error like "Uncaught ReferenceError: b is not defined"

This can be confusing, because it says not defined instead of not declared (Chrome)

Back to all JavaScript questions
Get LinkedIn Premium at Rs 399