Is hoisting of 'let' variables different for local and global scopes?

Viewed 50

I am trying out the code snippets below -

1)

console.log(a);
let a = 5;

I get - Uncaught ReferenceError: a is not defined at :1:13

2)

a = 10;
let a = 5;

I get - Uncaught ReferenceError: Cannot access 'a' before initialization at :1:3

If I try out the same snippets inside a function -

3)

function someName() {
    console.log(a);
    let a = 5;    
}
someName()

I get - Uncaught ReferenceError: Cannot access 'a' before initialization at someName (:2:17) at :1:1

4)

function someName() {
        a = 10;
        let a = 5;    
}
someName()

I get - Uncaught ReferenceError: Cannot access 'a' before initialization at someName (:2:11) at :5:5

Why is there a difference in the error messages for 1) and 2) but not 3) and 4). Is 'let' hoisting different for global and local scopes?

0 Answers
Related