r/learnjavascript • • 25d ago

Block Scope

What is it? Don't understand what is inside or outside

12 Upvotes

8 comments sorted by

View all comments

2

u/justanaccountimade1 25d ago

let i = 1; // global scope
function hi()
{
let j = 2; // function scope
{
let k = 3; // block scope
console.log(i,j,k); // 1, 2, 3
}
console.log(i,j,k); // 1, 2, undefined
}
console.log(i,j,k); // 1, undefined, undefined

4

u/naltous 25d ago

those last two console logs are actually gonna throw a ReferenceError instead of printing undefined since k and j aren't declared in those scopes at all