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

189
Views
why this closure and IIFE dont works?

Why this code dont print i and j variables?

(function f() {
  let i = 1;
  let j = 2;
  return () => {
    console.log(i);
    console.log(j);
  }
})()

I know, i know... this code yes, works:

let f = () => {
  let i = 1;
  let j = 2;
  return () => {
    console.log(i);
    console.log(j);
  }
}

f()();

Yes, the last code works, but I what to know why the first code doesn't works

Thanks a lot

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are returning a function (return () => {}) from the function but not calling it. You could simply just log the values in the function without returning a new function.

(function f() {
  let i = 1;
  let j = 2;
  console.log(i);
  console.log(j);
})();

Or call it like this.

(function f() {
  let i = 1;
  let j = 2;
  return () => {
    console.log(i);
    console.log(j);
  }
})()();

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!