I've done some research online, half of the articles suggest let/const/class are NOT hoisted, while the other half claim they are hoisted but are not initialized and ReferenceError is thrown when we try to access them before initialization.
So here is my question. Is 'let' hoisted or not?
a. If yes, then explain this:
x = 1;
let x;
console.log(x);
Output: ReferenceError: x is not defined.
Argument: If 'let' is hoisted then shouldn't let x; move to the top and hence when initialization takes place it should get the value 1 and hence 1 should get printed? But nope, its error.
b. If not then explain this:
var x = 1;
function print() {
console.log(x);
let x = 2
console.log(x);
}
print();
Output: ReferenceError: x is not defined.
Argument: If 'let' is not hoisted then output should've been
1
2
Please help!