Skip to main content

How to Resolve "TypeError: (intermediate value)(...) is not a function" Error in JavaScript

The TypeError: (intermediate value)(...) is not a function is a slightly cryptic error in JavaScript that almost always occurs because of a missing semicolon. It's a direct result of a feature called Automatic Semicolon Insertion (ASI), where the JavaScript engine misinterprets two separate lines of code as a single, invalid statement.

This guide will explain what this error means, how ASI causes it, and how to fix it by correctly using semicolons.

The Core Problem: Automatic Semicolon Insertion (ASI)

JavaScript has a feature where the engine automatically inserts semicolons at the end of lines where it thinks they are needed. While this allows you to omit semicolons in much of your code, it has a few tricky rules.

One of the most important rules is that the engine will not insert a semicolon if the next line starts with (, [, or `. Instead, it assumes the new line is a continuation of the previous one.

How the Error Occurs (The IIFE Example)

This error most frequently happens when you have an Immediately Invoked Function Expression (IIFE) or any function call that starts with ( immediately after a function expression without a semicolon.

Example of problem:

// Problem: There is no semicolon after the closing brace of `myFunction`.
let myFunction = function() {
return 'Hello'
}

// This line starts with a parenthesis.
(function() {
console.log('This is an IIFE');
})()

Because there is no semicolon after myFunction, the JavaScript engine sees the code like this:

let myFunction = function() {
return 'Hello'
}(function() { ... })()

The engine tries to:

  1. Invoke myFunction with the IIFE as an argument.
  2. myFunction returns the string 'Hello'.
  3. The engine then tries to invoke the result of that call ('Hello') as a function, because of the final ().
  4. This is equivalent to 'Hello'(), which is invalid because a string is not a function.

The "intermediate value" in the error message is the string 'Hello'—the value that was returned before the final, invalid function call.

Error Output:

Uncaught TypeError: (intermediate value)(...) is not a function

Solution: Use Semicolons

The solution is to ensure that your statements are correctly terminated with semicolons, preventing the parser from incorrectly merging lines.

Solution A (Best Practice): Add a Semicolon at the End

The most standard and readable solution is to add a semicolon at the end of the preceding statement.

// Correct: Add a semicolon after the function expression.
let myFunction = function() {
return 'Hello';
}; // <-- Semicolon added here

(function() {
console.log('This is an IIFE');
})();

Solution B: Add a Semicolon at the Beginning

A defensive practice, common in some coding styles, is to place a semicolon at the beginning of any line that starts with (, [, or `. This protects it from being merged with the previous line, regardless of whether that line had a semicolon or not.

let myFunction = function() {
return 'Hello'
}

// Correct: The leading semicolon terminates the previous statement.
;(function() {
console.log('This is an IIFE');
})();

Another Common Cause: Incorrect Chaining

The same error can occur if you try to chain function calls where the intermediate value is not a function.

Example of problem:

let user = {
getName: function() {
return 'Alice';
}
};

// Problem: .getName() returns a string. You cannot call .toUpperCase() on a string
// and then immediately call it again.
let result = user.getName().toUpperCase()(); // The final `()` is invalid.

Error Output:

Uncaught TypeError: (intermediate value)(...) is not a function

The "intermediate value" here is the uppercase string 'ALICE'. The code is trying to execute 'ALICE'(), which is invalid.

Solution: remove the extra, invalid function call.

// Correct: Remove the final pair of parentheses.
let result = user.getName().toUpperCase();
console.log(result); // Output: 'ALICE'

Conclusion

The TypeError: (intermediate value)(...) is not a function is almost always caused by a missing semicolon that leads the JavaScript engine to incorrectly combine two lines of code.

To solve it:

  • Check the line before the error: The problem is usually a missing semicolon at the end of the statement right before the line that throws the error.
  • Be careful with lines starting with ( or [: These are the most common triggers for this ASI-related bug.
  • Use a code formatter (like Prettier): A good code formatter will automatically insert semicolons in the correct places, preventing this entire class of errors.