-1

We can pretend for using unary operator as an IIFE like below:

+function(){
   return 5;
}();//5


-function(){
   return 5;
}();//-5


~function(){
   return 5;
}();//-6


!function(){
   return 5;
}();//false

So, is there real-world use case for such IIFE or we should avoid using IIFE by using unary operator and just use real IIFE:

(function(){
   return 5;
}()); //5
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 2
    It makes absolutely no difference, so use whichever one you think is the most beautiful. – Pointy Sep 27 '14 at 18:53
  • Using unary operator makes difference while returning result and so I think there must be beauty of programming... – Bhojendra Rauniyar Sep 27 '14 at 18:54
  • All of those pieces of code do exactly the same thing: run the function and throw away the result. The unary operator (or the surrounding parentheses) are required for that to work, but they all do the same thing. – Pointy Sep 27 '14 at 18:55
  • Yes there is one not in code execution but in character length, some minifiers actually use this as you save 1 character by using !,+,-, etc. as a prefix since it allows the removal of the wrapping parens. – axelduch Sep 27 '14 at 18:58
  • 1
    What on earth are you talking about – Lightness Races in Orbit Sep 27 '14 at 19:03
  • +1, even though it's useless, it's cool – Winchestro Sep 27 '14 at 19:40

2 Answers2

1

My IDE is not happy about the +function() thing:

enter image description here

and so are JSLint and JSHint. It's better to avoid constructs like these because:

  • they confuse the reader
  • they confuse IDEs and validators
  • you cannot return anything meaningful from such an IIFE (for example, an object, which is by far the most frequent use case).
georg
  • 211,518
  • 52
  • 313
  • 390
-1

The only advantage of using IIFE with operator to me is if I forget to use trailing semi-colon that won't cause a problem.

But using IIFE without operator is if I forget to use trailing semi-colon that would be a problem.

(function(){
...
}()) //no semi-colon
(function(){
...
}());

The preceding code would be interpreted as a function call. (first IIFE would be the function to be called and second would be the parameter)


Since as of the @georg answer there's no meaningful return from such IIFE in most use case I would choose to use IIFE with operator.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 2
    I don't know why this has been down-voted. The english is imperfect, but the answer is the correct one: `(fn-expr)` in JavaScript can be concatenated with a previous line according to the ASI rules, which is dangerous; generally speaking, you are advised to manually include a semicolon *before* lines opening with parentheses or brackets. In the case of IIFEs, the only purpose of the parentheses are to force function *expression*-parsing instead of *statement*-parsing; and using a unary op achieves this in an ASI environment more reliably than parens do. – ELLIOTTCABLE Feb 21 '16 at 03:34
  • @ELLIOTTCABLE I'd guess it was downvoted for the statement "if I forget to use trailing semi-colon that won't cause a problem". Using confusing, non-standard hacks to circumvent the need for a semi-colon seems a bit insane. – Dan Bechard May 26 '16 at 13:49
  • It's not insane, Dan, it's perfect standard practice. Look through large JavaScript projects for StatementListItems containing only a single UnaryExpression. (Also, please refrain from using words like ‘insane’ as a pejorative; it's ableist and pretty shitty.) – ELLIOTTCABLE May 26 '16 at 14:00
  • (Even *ignoring* your language, and your unfamiliarity with unary-selection of FunctionExpression productions, you must at least realize that reliance upon ASI is **absolutely** standard practice in our industry? Whether or not you personally choose to use it, those that *do* so choose, will often go to great lengths to avoid unnecessary semicolons … this isn't ‘insane,’ it's effort towards a consistency of code-style, which is an unequivocal Good Thing in a large, shared codebase.) – ELLIOTTCABLE May 26 '16 at 14:00