1

I am new to JavaScript and I am trying to understand why a variable declaration ends with "()": "var counter = ( ..... )();". If "()" are removed, it does not work but, what is their purpose? What do they tell JavaScript to do?

Se the code in this link.

https://www.tutorialsteacher.com/codeeditor?cid=oojs-64

Thanks!

  • 2
    `()` will execute a function. So, inside `( ..... )` you will have a function, it will be executed and `counter` would be the return value of said function. – VLAZ Jun 23 '20 at 23:04
  • https://developer.mozilla.org/en-US/docs/Glossary/IIFE – mgm793 Jun 23 '20 at 23:05
  • 1
    because the function is an `Immediately-Invoked Function Expression (IIFE)` – Sven.hig Jun 23 '20 at 23:07
  • Relevant: [What is this JavaScript pattern called and why is it used?](https://stackoverflow.com/q/26092101) | [Why assign an IIFE to a variable?](https://stackoverflow.com/q/26451697) | [What is the purpose of a self executing function in javascript?](https://stackoverflow.com/q/592396) | – VLAZ Jun 23 '20 at 23:11
  • 1
    Ok, I see it. Thanks for pointing a newbie in the right direction. I have not seen this in my classes. – Armando De Leon Jun 23 '20 at 23:14

0 Answers0