Block Scope Functions

ES6 Block Scope is The new IIFE. instead of using IIFE we can now use block-scope.

// Old
(function IIFE() {
  console.log('I am an IIFE');
})();
 
// New
{
  console.log('I can now used block scope')
}
 
// Example
 
const Buttons = function Buttons() {
 
  const noButtons = 5;
 
  { // protected IIFE
     for(let i = 0; i <= noButtons; i++ ) { 
        document.body.innerHTML += `<button id=${i}>I am a button</button>`;
        let elem = document.getElementById(i);
        elem.addEventListener("click", function() {
          elem.innerHTML = "I have been clicked"; 
        });
     } 
  } 
 
}
 
Buttons();
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License