Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda