10

I noticed a + sign preceeding a function in the bootstrap.js code and wondered what effect this has or is it just for fun?

twitter bootstrap

Timmerz
  • 5,746
  • 5
  • 29
  • 44
  • 2
    Ugh... pictures of text. Mind if I edit it to just text? – Corey Ogburn Dec 03 '13 at 18:25
  • @CoreyOgburn no need, it's a duuuuuuupe. – Mathletics Dec 03 '13 at 18:26
  • The answer also seems to be located here: https://stackoverflow.com/questions/5827290/javascript-function-leading-bang-syntax – Corey Ogburn Dec 03 '13 at 18:26
  • the plus operator coerces the expression to its right into a number. if there is a "([args])" after the function statement, then the whole thing becomes an expression (instead of a syntax error). the function is then executed to return a value that it then attempts to coerce into a number. if that doesn't work, no error is shown, so it "works" fine. a paren is much more readable... – dandavis Dec 03 '13 at 18:37

1 Answers1

10

I think it forces the parser to treat the part following the + as an expression. You can also read Immediately-Invoked Function Expression

Also check JavaScript plus sign in front of function name

Community
  • 1
  • 1
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299