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

190
Visualizações
If I call a function in the head section of JavaScript for-of loop, is it guarantied to be called only once?

I have tried the code below in Firefox and Chrome:

function test() {
    console.log("***")
    return [1,2,3]
}

for (const t of test()) {
    console.log(t)
}

The test function was called only once. Is it the standard behavior for this kind of loop? Is it like the initialization part of for ([initialization]; [condition]; [final-expression]) loop? I didn't find the explanation on MDN and I didn't understand much from ECMA standard.

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

0

The ECMAScript specification splits the runtime semantics of the for..in and for..of construct into two parts:

ForInOfStatement : for ( LeftHandSideExpression of AssignmentExpression ) Statement

  1. Let keyResult be ? ForIn/OfHeadEvaluation(« », AssignmentExpression, iterate).
  2. Return ? ForIn/OfBodyEvaluation(LeftHandSideExpression, Statement, keyResult, iterate, assignment, labelSet).

So we first have the "head" evaluation (involving the "AssignmentExpression" at the right of of), and after that, the "body" evaluation, which uses the keyResult (not the AssignmentExpression).

The "head" evaluation involves the actual evaluation in step 3 of that procedure:

  1. Let exprRef be the result of evaluating expr.

The "body" evaluation involves the actual iteration in step 6:

  1. Repeat
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