Code patterns for anonymous function

There are several ways to write anonymous function

The Bad Pattern : It won't work and you'll get a syntax error

<script> function(){ alert(1); }(); </script>

Pattern 1 : Function Literal

Declare Function as an Object first then execute it.

<script> (function(){ alert(1); } ) ( ); </script>

Pattern 2 : Prior Expression

Use parentheses to force a declared function to be executed since JavaScript evaluates expressions from the inner parentheses to outer parentheses

<script> ( function(){ alert(2); } ( ) ); </script>

Pattern 3 : Void Operator

We can use "void" to evaluate a lone operand without using parentheses as the wrapper. of function

<script> void function(){ alert(3); }() </script>

Conclusion

Technically these three patterns are equivalent, but I do prefer the pattern 3 for my own reasons.

You may review these patterns and see which make sense most for you.