1

I know about self-invoking JavaScript functions made famous by jQuery: ( function(x){ console.log(x);})('Hello World'); logs Hello World.

I recently came across a code where the author uses 0 within the main parentheses and before the function.

example:

(0, function(x){ console.log(x);})('Hello World');

This will work and log 'Hello World' but I was curious to know what is point of the parameter (in this case 0) before the function? what is the use case for it?

In the case i saw the function was in variable:

f = function(x){ console.log(x);};
 (0, f)('Hello World');
melpomene
  • 84,125
  • 8
  • 85
  • 148
nomar
  • 11
  • 1
  • 1
    It's not a parameter. It's the comma operator at work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator. The actual code might have been something like `(0, foo.bar)(...)`. This is done to make `this` inside `foo.bar` *not* refer to `foo`. – Felix Kling Jan 16 '18 at 16:16
  • Than you Flix! It might be long story but wondering what makes `this` in `foo.bar` in this expression `(0, foo.bar)(...) ` not point to `foo`? – nomar Jan 16 '18 at 17:24
  • Quentin, can you please post the link to duplicate question? – nomar Jan 16 '18 at 17:25
  • Quentin, Never mind. I see the link – nomar Jan 16 '18 at 17:26

0 Answers0