In javascript, compilation phase find and associate all declarations with their appropriate scopes.
CASE1
a = 2;
console.log(a);
If not used in strict mode the snippet is assumed to be actually processed as:
var a;
a = 2;
console.log(a);
Output: 2
CASE2
if the declaration happens below console.log()
console.log(a);
var a = 2;
Here at line 1: compiler will check for the declaration of a in its scope if not found, the global scope will assign one and the same code will be assumed again same as
var a;
console.log(a);
a = 2;
Output : undefined // as no value is assigned while interpreter read console
CASE3
console.log(a);
a = 2;
Output: RefErr
Why this throws reference error and not undefined?