-
undefined
- It's a primitive data type in Javascript.
- Any variable which has been declared but has not been assigned any value has the value undefined.
- During the memory creation phase of the execution context, every variable is assigned the value of
undefinedby the JS Engine.
-
null
- It's a primitive data type in Javascript.
- A variable can be assigned the value
null. - Although
nullis itself a data type, but on checking it's type usingtypeofwe get"object".
- Why is typeof null “object”?
-
undeclared (not defined)
- This occurs when we try to access a variable which hasn't been declared or initalized yet.
- If we try to
console.log(undeclaredVar)the error will be raised as shown above, so especially for this a try{...} catch(e){...} block has to be written. But if we instead use"use strict"pragma we can fix these errors during compile phase itself rather than during runtime. -
typeofoperator is the only operator that can reference a variable that hasn't been declared and not throw an error.typeof undeclaredVar //"undefined"
- This occurs when we try to access a variable which hasn't been declared or initalized yet.
-
uninitialized (TDZ)
- It was introduced with ES6.
- It's based on the concept of Temporal Dead Zone.
- Variables which are block scoped (declared using
letorconst) are initially uninitialized. They can't be accessed unless they're in the temporal dead zone or intialized.
References & Further Reading:
Top comments (0)