title: Javascript - self-executing functions : why to use them if I can create local scope with not self-executing functions?
body: <p>I know there are a lot of posts here and elsewhere about selfexecuting functions but I still have some questions after reading posts.</p>
  
  <ol>
  <li><p>why would I ever assign a self-executing function to a variable? If seems that they execute themselves anyways. </p>
  
  <pre><code>var myFunc=(function() {
   console.log('Hello World');
  })();
  </code></pre></li>
  <li><p>I read a lot that the reason to use self-executing functions is to keep variables private. If I have a not self-executing function, everything I define inside that function is gonna be private anyways?!</p>
  
  <pre><code>(function() {
   var name="my Name"
   console.log(name);
  })();
  
  vs.
  
   function() {
   var name="my Name"
   console.log(name);
   };
   //its the same
  </code></pre></li>
  </ol>
  
  <p>So I dont quite understand how self-executing functions are to keep local scope (as you can do that using not self-executing functions) so the only reason I see is to use them when you want to execute automatically for example on page load.</p>
  
  <p>Thanks!</p>
  
  <p>just one more question:</p>
  
  <pre><code>var test=(function myFunc(){
        var name="Hello World"
        return {
          test1: function(){
            return name;
          },
          test2:function(){
            return name+"1"
          }
        }
      })()
  
      test.test1()
  
  vs
  
      var test=function myFunc(){
        var name="Hello World"
        return {
          test1: function(){
            return name;
          },
          test2:function(){
            return name+"1"
          }
        }
      }
  
      test.test1()
  </code></pre>
  
  <p>--> what exactly happens here that because of IIFE I can actually execute test.test1() and not with a regular function?</p>