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

114
Views
IIFE vs scope braces

Perhaps this is the stupidest question, but for IIFE, it mentions being used as a scope that would not pollute the global namespace.

Why would that be necessary as opposed to just using braces by itself to delimit scope (like in C)? For example:

{
    let firstVariable = 1;
    let secondVariable = 2;
}
console.log(firstVariable);

Instead of:

(function () {
    let firstVariable = 1;
    let secondVariable = 2;
})();
console.log(firstVariable);

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

0

JavaScript first appeared in 1995. The first specification edition was from 1997.

The specification that does include block scope is ES6 from 2015. Therefore, there has been no block scope for close to 20 years before it was introduced. IIFEs have existed in the mean time.

Also worth noting that block scope is not universal. It does work for let and const but not var

{
  var foo = 42;
}

console.log(foo); //output: 42

(function() {
  var bar = 42;
})();

console.log(bar); // error because `bar` does not exist in this context

Functions are also block-scoped as of ES6 but the rules around that are weird.

console.log("---- foo ----");

console.log("before block", foo); //output: undefined
{
  function foo() { return 42; }
}
console.log("after block", foo);  //output: function foo() { return 42; }


console.log("---- bar ----");

function bar() { return 1; }

console.log("before block", bar());   //output: 1
{
  function bar() { return 2; }
  console.log("inside block", bar()); //output: 2
}
console.log("after block ", bar());   //output: 2


console.log("---- baz ----");

{
  function baz() { return 1; }

  console.log("before inner block", baz());   //output: 1
  {
    function baz() { return 2; }
    console.log("inside inner block", baz()); //output: 2
  }
  console.log("after inner block ", baz());   //output: 1
}
.as-console-wrapper { max-height: 100% !important }

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!