0

This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,

//Self-evoking anonymous functions
((function () {

    alert("hi");

})());

What's the difference with no wrap,

alert("hi");

I still get the same result - hi

What can you pass/ put in the brackets in the end bit - })());? and why?

Run
  • 54,938
  • 169
  • 450
  • 748

1 Answers1

1

Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99