0
var output = (function(x) { 
//do something with x 
return x; })
(0);

I don't get what x is being passed in as or where its being passed in. Maybe this is not written correctly.

Mpsteve137
  • 49
  • 1
  • 1
  • 4
  • 4
    `x` is the argument, in this case `0`. It's passed when the function is called (`(0)` as in `fn(0)` - but instead of first assigning the function expression to a variable `fn`, it is used without a name and immediately called, just like I can write `'abc'.toUpperCase()` without first assigning `'abc'` to some variable). The name of this construct is **IIFE** (immediately-invoked function expression). It is sometimes used to create an isolated scope and to allow easy short-circuiting with `return`. – CherryDT Aug 09 '21 at 15:16
  • 1
    MDN Web Docs: [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) – lejlun Aug 09 '21 at 15:18
  • IIFE with function expression so to say – ratyacatnug Aug 09 '21 at 15:35
  • @CherryDT thank you for the response, makes a lot more sense now. – Mpsteve137 Aug 09 '21 at 15:38
  • @ratyacatnug "immediately-invoked function expression with function expression"...? - I think you fell victim to the [RAS syndrome](https://en.wikipedia.org/wiki/RAS_syndrome) here ;-) – CherryDT Aug 09 '21 at 15:44
  • @CherryDT sorry bro. in addition its main usage in 'module' patern and writing safe code, in other means encapsulation – ratyacatnug Aug 09 '21 at 15:52

0 Answers0