Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
How Javascript let keyword prevents re-defining?

I'm python developer. In python, When you want to check whether a variable is already defined, you use dict

memo = {}
if 'a' not in memo:
    memo['a'] = 'var_a'

javascript let keyword has similar functionality, it prevents re-defining.

let a = 1;
let a = 2; // SyntaxError

So how let achieves this functionality under the hood? Does V8 engine keeps some kind of HashMap of let variables?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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 vars 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).
    ...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda