Why do you need to invoke an anonymous function on the same line?

I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works – everytime I was just told to use it…: // Create a new anonymous function, to use as a wrapper (function(){ // The variable that would, normally, be global var msg = “Thanks for visiting!”; … Read more

Explain the encapsulated anonymous function syntax

Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn’t: function(){}();? What I know In JavaScript, one creates a named function like this: function twoPlusTwo(){ alert(2 + 2); } twoPlusTwo(); You can also create an anonymous function and assign it to a … Read more