Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

TypeError: "x" is not a function

Naši dobrovolníci ještě tento článek do jazyka Čeština nepřeložili. Přidejte se a pomozte nám tuto práci dokončit!

Message

TypeError: "x" is not a function

Error type

TypeError.

What went wrong?

It was attempted to call a value like a function, but the value is not actually a function. Some code expects you to provide a function, but that didn't happen.

Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function? For example, JavaScript objects have no map function, but JavaScript Array object do.

There are many built-in functions in need of a (callback) function. You will have to provide a function in order to have these methods working properly:

Examples

A typo in the function name

In this case, which happens way too often, there is a typo in the method name:

var x = document.getElementByID("foo");
// TypeError: document.getElementByID is not a function

The correct function name is getElementById:

var x = document.getElementById("foo");

Function called on the wrong object

For certain methods, you have to provide a (callback) function and it will work on specific objects only. In this example, Array.prototype.map() is used, which will work with Array objects only.

var obj = { a: 13, b: 37, c: 42 };

obj.map(function(num) {
  return num * 2;
});

// TypeError: obj.map is not a function

Use an array instead:

var numbers = [1, 4, 9];

numbers.map(function(num) { 
  return num * 2; 
}); 

// Array [ 2, 8, 18 ]

See also

Štítky a přispěvatelé do dokumentace

 Přispěvatelé této stránky: fscholz
 Poslední aktualizace od: fscholz,