JavaScript plus sign in front of function name

javascript in front of function
javascript variable with plus sign
in front of a function
before a function in js
react plus sign
angular plus sign
javascript exclamation mark after function
iife pattern javascript

I've been looking on info about self-invoking functions, and somewhere I stumbled on this notation:

+function(){}

Can someone explain to me what the + sign in front of the function means/does?


It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.:

+function() { console.log("Foo!"); }();

Without the + there, if the parser is in a state where it's expecting a statement (which can be an expression or several non-expression statements), the word function looks like the beginning of a function declaration rather than a function expression and so the () following it (the ones at the end of the line above) would be a syntax error (as would the absense of a name, in that example). With the +, it makes it a function expression, which means the name is optional and which results in a reference to the function, which can be invoked, so the parentheses are valid.

+ is just one of the options. It can also be -, !, ~, or just about any other unary operator. Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically):

(function() { console.log("Foo!"); })();
// or
(function() { console.log("Foo!"); }());

JavaScript plus sign in front of function name, It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.: +function()  Javascript Web Development Front End Technology The +function() {} notation is primarily used to force the parser to treat whatever follows the + as an expression. This is used for functions that are invoked immediately, for example,


Subsidiary to @TJCrowder's answer, + is usually used to force numerical casting of a value as this SO answer explains. In this instance it is called the 'unary plus operator' (for ease of googling).

var num = +variant;

So in front of a function it can be a way to force the function's result to be interpreted as a number. I doubt it happens yet, but theoretically the JIT could use that to compile the function as a numerical-only function etc. However, to prevent the unary plus being a concatenation when used in a larger expression, you would need parentheses:

blah + (+(function(){ var scope; return "4"; })());

Why do we use a plus sign in front of function name in JavaScript?, The +function() {} notation is primarily used to force the parser to treat whatever follows the + as an expression. This is used for functions that  In this instance it is called the 'unary plus operator' (for ease of googling). var num = +variant; So in front of a function it can be a way to force the function's result to be interpreted as a number. I doubt it happens yet, but theoretically the JIT could use that to compile the function as a numerical-only function etc.


So the short answer is that it prevents a syntax error, by using the function results in one way or another.

You can also instruct the engine that you're not even interested in the return value by using the void operator:

void function() { console.log("Foo!"); }();

Of course, putting braces around the whole thing also serves that purpose.

javascript tutorial - Javascript plus sign in front of function name, Javascript plus sign in front of function name - It forces the parser to treat the part following the + as an expression. This is usually used for functions that are  possible duplicate of What does the plus sign do in 'return +new Date' – nawfal Jan 10 '14 at 12:36. Same issue but the title of other page doesn't describe the general case as this does – geotheory Aug 30 '15 at 10:12. possible duplicate of What does +… mean in JavaScript – Bergi Aug 8 '17 at 17:02.


JavaScript plus sign in front of function name, It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.: These tools are often used as part of a JavaScript build pipeline to reduce the size of a program prior to deploying it to production. Such transformations often change a function's name at build-time.


What does a plus sign mean in front of a function in JavaScript , +function` doesn't have any special meaning in JavaScript, and that's not what people are using it for. What they're using is most likely the  The dollar sign $ was chosen for the function name by the first of these libraries because it is a short one-character word, and $ was least likely to be used by itself as a function name and therefore the least likely to clash with other code in the page.


JavaScript plus sign in front of function name - javascript, In this instance it is called the 'unary plus operator' (for ease of googling). var num = +variant; So in front of a function it can be a way to force the function's result  JavaScript functions are not polymorphic the way that functions in many other languages are in that JavaScript doesn't care if the actual arguments defined for the functions are different as it can't distinguish between them on that basis.