0

Reading some third party code, a self invoking function starts with !function. What is the meaning of the exclamation mark in this context?

!function(){ // not assigned to variable or returned
  //code here

}()
Skarlinski
  • 1,991
  • 7
  • 26
  • 4
    Oops, I mean this one: https://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function – melpomene Oct 08 '17 at 15:19
  • It does a logic NOT (the `!`) on the result of the self invoking function. – Dmitry Oct 08 '17 at 15:19
  • 1
    it forces the following expression to evaluate. (but only for this case, `void` is the statement of choice.) – Nina Scholz Oct 08 '17 at 15:19
  • 1
    it's one character shorter than wrapping the whole function expression in parentheses before calling it. – Touffy Oct 08 '17 at 15:20
  • 1
    and it also prevents the line being misinterpreted as a continuation of the previous one if there wasn't a semicolon. Lines starting with a parens can be risky so if you count the semicolon, this is TWO whole characters shorter! :D – Touffy Oct 08 '17 at 15:22
  • Putting this character there makes it a function expression. – Amit Kumar Singh Oct 08 '17 at 15:34

0 Answers0