The correct answer is A , not D.
Inside myFunction(), the declaration:
var b = 1;
is hoisted to the top of the function scope, but only the declaration is hoisted, not the assignment. So internally, JavaScript treats the function approximately like this:
function myFunction() {
var b;
a = a + b;
b = 1;
}
Now examine this line:
a = a + b;
Before JavaScript can assign a value to a, it must first evaluate the right-hand side:
a + b
At this point:
b
exists locally because var b was hoisted, so its value is:
undefined
However:
a
has not been declared anywhere. Reading an undeclared variable causes a:
ReferenceError
So this line throws an error before assignment happens:
a = a + b;
Because the error occurs on that line, the next line inside the function is never executed:
var b = 1;
Also, because the error is not handled with try...catch, the program stops before reaching:
console.log(a);
console.log(b);
Therefore, the accurate statement is:
Line 02 throws a reference error, therefore line 03 is never executed.
So the verified answer is A .