There can be a couple of reasons.
The first one is protecting the scope, as the function creates a new scope.
Another one can be binding variables, for example
for (var i = 0; i < n; i++) {
element.onclick = (function(i){
return function(){
// do something with i
}
})(i);
}