Why would one write global code inside a function definition-call pair?

Posted by ssg on Stack Overflow See other posts from Stack Overflow or by ssg
Published on 2010-05-24T15:14:32Z Indexed on 2010/05/24 15:21 UTC
Read the original article Hit count: 154

Filed under:

I see examples where JavaScript code including jQuery and jslint use the notation below:

(function(){
  // do something
})();

instead of:

// do something

I first thought this is just for local scoping, i.e. creating local variables for the code block without polluting global namespace. But I've seen instances without any local variables at all too.

What am I missing here?

© Stack Overflow or respective owner

Related posts about JavaScript