Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!