My question is when i declare a variable of type let or const inside a block like that
//does JS setup a memory space for let a when we are here ? { //or when we are inside the block JS setup the memory space for let a ? let a = 5; } will it get hoisted only when we get to the block(at execution time) or will it get hoisted at the beginning ?
because when i call it outside of the block the error is different than the one inside of the block
console.log(a); ---> "Uncaught ReferenceError: a is not defined" { console.log(a); -----> "Uncaught ReferenceError: Cannot access 'a' before initialization" let a = 5; } the error outside of the block is the same like a variable that truly doesn't exist
same error
console.log(a); ---> "Uncaught ReferenceError: a is not defined" console.log(thisVariableDoesntExists); ---> "Uncaught ReferenceError: thisVariableDoesntExists is not defined" { let a = 5; } so again my question is will Javascript setup a memory space for the variable let a = 5 when we are inside of the block or when the execution context start the creation phase ?
because from what it seems to me javascript will only setup a memory space for let only when we enter the block
DISCLAIMER: if you didn't clearly understood my question DO NOT think that my question was about why when i console.log(a) i don't get the "undefined" like the var but instead i get an error. that is not my question !
Thanks in advance for all the helpers :)
Variables defined with let and const are hoisted to the top of the block, but not initialized. Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared.letandconstare block-scoped. "let and const declarations define variables that are scoped to the running execution context's LexicalEnvironment. The variables are created when their containing Environment Record is instantiated but may not be accessed in any way until the variable's LexicalBinding is evaluated. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer's AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. [...]"