Asked • 03/27/19

Can someone explain this JavaScript auto executing function?

var foo = (function(){ var x = 0; return function(){return x++;}; })() Why the var x = 0 expression only runs once is my biggest misunderstanding about this.

2 Answers By Expert Tutors

By:

Sparky E. answered • 07/08/19

Tutor
4.8 (26)

Software Engineer @ J.P. Morgan Chase & Co.

Ian M. answered • 03/27/19

Tutor
5.0 (2,509)

JavaScript, Web Application Engineer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.