Does V8 engine keeps some kind of HashMap of let variables?
Essentially, yes - though it's not a V8 thing in particular, it's something that all spec-compliant JavaScript engines must adhere to. It's called an environment record, which in this case is a "lexical environment", which is, to oversimplify, a collection of identifiers for a given scope and the values that the identifiers hold at that point.
When a function is evaluated, FunctionDeclarationInstantiation runs, which does, among many other things:
34. For each element d of lexDeclarations, do
a. (unimportant; omitted)
b. For each element dn of the BoundNames of d, do
i. If IsConstantDeclaration of d is true, then
1. Perform ! lexEnv.CreateImmutableBinding(dn, true).
ii. Else,
1. Perform ! lexEnv.CreateMutableBinding(dn, false).
The lexDeclarations
include identifiers declared with let
or const
inside the function. This list cannot have duplicate entries as a result of an Early Errors requirement, which says, for blocks:
It is a Syntax Error if the LexicallyDeclaredNames of StatementList contains any duplicate entries.
Identifiers declared with var
s are slightly different - they're not LexicallyDeclaredNames, but VarDeclaredNames instead, which have no requirement against being listed multiple times in a block. This doesn't result in duplicate var
names being created multiple times in the environment because the duplicate declarations are explicitly skipped from the CreateMutableBinding
call:
e. For each element n of varNames, do
i. If n is not an element of instantiatedVarNames, then
1. Append n to instantiatedVarNames.
2. Perform ! varEnv.CreateMutableBinding(n, false).
...