Have seen many usage in this format:
(0, function(a, b){console.log(a + b)})(3, 4)
What is 0
for?
Have seen many usage in this format:
(0, function(a, b){console.log(a + b)})(3, 4)
What is 0
for?
That 0
is ignored.
That comma is the comma operator:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
Basically that 0
is evaluated as 0
then that function expression is evaluated and since the function is the last thing evaluated in the comma list a function reference to it is returned. It is that returned function that is called.
In the code in your question, the 0
is therefore completely useless.
However, it could be that the author intended this instead:
0,function(a, b){console.log(a + b)}(3, 4);
Which saves typing two characters (
and )
but then it takes two characters anyway so it's not very useful.
It serves the same purpose as a parenthesis or a bang prefix.
0
is an expression that has no effect. The comma operator puts the right hand side into experssion context.
This means that function
is then treated as the start of a function expression (which can be an IIFE) instead of a function declaration (which would throw an error if you tried to treat it as an IIFE, which this code does by following it with (3, 4)
).