“TypeError: (intermediate value)(…) is not a function” in JS – How To Fix?

TypeError (intermediate value)(…) is not a function in JS

The error “TypeError: (intermediate value)(…) is not a function” in JS often occurs when we forget a semicolon after a function declaration to separate the expressions. In this tutorial, I will show you how to fix it with just some simple steps.

What causes this error?

Reproduce the “TypeError: (intermediate value)(…) is not a function” error in JavaScript

Let’s take a look at example 1:

const myFunc = function (a, b) {
 console.log('a + b =', a+b);
 console.log('|a - b| =', Math.abs(a-b));
 console.log('a * b =', a*b);
 console.log('a / b =', a/b);
 console.log('a % b =', a%b);
 console.log('a ^ b =', a**b);
}

(function (result) {
  myFunc(4,8);
})();

Output: 

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

At first, it seems that there is nothing wrong with our code. But if we take a closer look at line 8, we can see that we are missing a semicolon after declaring our function. 

At this time, you might say: “Wait, but the semicolon in JavaScript is optional, isn’t it?” Well, the answer is, sometimes, but not all the time. The semicolon in JavaScript is not always necessary in JavaScript, thanks to the Automatic Semicolon Insertion (ASI). ASI is a set of rules that JavaScript will decide if, in a specific spot, the semicolon will be interpreted or not. This is somewhat helpful since we don’t always have to include a semicolon after each line of our code. But there are situations where ASI will be triggered. For example: 

var a = 1
var b = 2
var c = 3
var d = 4

sum = a + b
(c+d).valueOf();

We will get this error: 

b is not a function

This is because ASI will understand our code as: 

sum = a + b(c+d).valueOf();

The same problem happens to our example 1. ASI will understand our code as: 

const myFunc = function (a, b) {
 console.log('a + b =', a+b);
 console.log('|a - b| =', Math.abs(a-b));
 console.log('a * b =', a*b);
 console.log('a / b =', a/b);
 console.log('a % b =', a%b);
 console.log('a ^ b =', a**b);
} (function (result) {
  myFunc(4,8);
})();

And therefore, causing the problem.

How to fix “TypeError: (intermediate value)(…) is not a function” error in JavaScript?

Method 1: Adding the semicolon

The simplest way to solve this problem is to add the semicolon in the place where the error occurs. 

For example 1, we will add the missing semicolon in line 8, after declaring the function: 

const myFunc = function (a, b) {
 console.log('a + b =', a+b);
 console.log('|a - b| =', Math.abs(a-b));
 console.log('a * b =', a*b);
 console.log('a / b =', a/b);
 console.log('a % b =', a%b);
 console.log('a ^ b =', a**b);
};    

(function (result) {
  myFunc(4,8);
})();

Output

a + b = 12
|a - b| = 4
a * b = 32
a / b = 0.5
a % b = 4
a ^ b = 65536

With only adding a single semicolon, our program has run correctly!

Method 2: Using semicolons as a habit

ASI has allowed us to not focus too much on the semicolons and reduce some ‘noise’ in our JavaScript code. However, consistency should be more concerned. Therefore, if we don’t want our code to run into those unnecessary problems, we should practice using the semicolon after each line as a habit. 

Summary

TypeError: (intermediate value)(…) is not a function” is a common error in JavaScript for people who rely on ASI. Fortunately, the solution is quite simple: add the semicolon where the error occurs. Moreover, every time you code, you should try to practice adding the semicolon after each line to make it a habit.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *