JavaScript
Function Expression
Coding
Web Development
JavaScript Operators

JavaScript plus sign in front of function expression

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When you see a plus sign in front of a JavaScript function expression, it is usually not there for arithmetic. Most often, it is being used as a unary operator to force the parser to treat the following function as an expression, which makes an immediately invoked function expression possible.

Why +function() { ... }() Exists

At the start of a statement, function foo() {} is normally parsed as a function declaration, not as a function expression. But an immediately invoked function needs an expression.

Prefixing the function with a unary operator changes the parse context.

javascript
+function () {
  console.log('runs immediately');
}();

The unary + tells the parser, "what follows is an expression." That allows the function expression to be created and then immediately invoked.

Other operators can do the same job:

javascript
1!function () {
2  console.log('also an IIFE');
3}();
4
5(function () {
6  console.log('parentheses are the most common form');
7}());

The parenthesized version is more common today because it communicates intent more clearly.

Unary Plus Also Coerces the Result to a Number

The + operator still keeps its normal unary meaning after the function runs: it tries to convert the resulting value to a number.

javascript
1const value = +function () {
2  return '42';
3}();
4
5console.log(value); // 42

So in a case like this, two things are happening:

  1. the function is forced into expression form
  2. the return value is coerced to a number

That second effect is often incidental. Many older examples use unary + just to trigger expression parsing, not because numeric conversion was especially desirable.

Prefer Clearer Modern Forms

In modern JavaScript, the most readable form of an IIFE is usually just the parenthesized expression.

javascript
1(function () {
2  const config = loadConfig();
3  console.log(config);
4}());

Or, with an arrow function:

javascript
(() => {
  console.log('modern IIFE');
})();

These forms make the intent obvious without introducing extra numeric-coercion semantics.

When You Might Still Encounter Unary Plus

You are most likely to see +function() { ... }() in older code, minified code, or code golf style examples. It is valid JavaScript, but it is not usually the most maintainable spelling in application code.

If you see it in a code review, the right explanation is not "plus runs the function." The function runs because it is an expression followed by (). The plus merely helps establish that expression context and then coerces the return value.

You may also encounter other unary operators such as !, ~, or void used the same way. They all force expression parsing, but that does not mean they are equally readable or equally appropriate in production code.

If a reader has to stop and remember the parser trick before understanding the line, the syntax is probably too clever for everyday application code. That readability cost is the main reason many teams avoid these forms in modern codebases.

Common Pitfalls

Thinking the unary plus is required for all function expressions is incorrect. It is only one way to force expression parsing.

Explaining +function() { ... }() purely as numeric conversion misses the more important parser role in IIFE syntax.

Using unary plus for IIFEs in new code can hurt readability compared with the parenthesized form.

Summary

  • A plus sign before a function expression is usually a unary operator that forces expression parsing.
  • This pattern is often used to create an immediately invoked function expression.
  • Unary plus also coerces the function's return value to a number.
  • In modern code, parenthesized IIFEs are usually clearer than the unary-plus form.

Course illustration
Course illustration

All Rights Reserved.