JavaScript: Uncaught TypeError: n is not a function
Last Updated :
09 Sep, 2024
A TypeError occurs in JavaScript when you attempt to execute something that is not a function, often due to incorrect initialization or typos. This means the expression you tried to call did not resolve to a function object.
Message:
TypeError: Object doesn't support property or method {n} (Edge)
TypeError: "n" is not a function
Error Type:
TypeError
What Causes TypeError: "n" is not a function:
Example 1: In this example the script attempts to use document.getElementByID, which is a typo. The correct method is document.getElementById, causing a TypeError due to the incorrect method name.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
let n = document.getElementByID('GFG');
document.write(n);
</script>
</body>
</html>
Output: While running the above example, it throws a JavaScript error.
TypeError: document.getElementByID is not a function
Note: The correct function name is getElementById
Example 2: In this example the script tries to call num.fo(), but fo() is undefined, leading to a TypeError. The correct method is num.foo(), which would log "foo called" to the console.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
let num = {
foo: function () {
console.log("foo called");
}
};
num.fo();
</script>
</body>
</html>
Output:
TypeError: num.fo is not a function
Note: The correct function call is foo().
Example 3: This script causes a TypeError because arr is an object, not an array. The .map() method is for arrays, so using it on an object will fail.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
let arr = { a: 1, b: 2, c: 3 };
arr.map(function (num)){
return num;
});
</script>
</body>
</html>
Output:
TypeError: arr.map is not a function
The correct way is to declare the array properly like arr=[35,42,90]
Example 4: This script causes a TypeError because the name property is both a string and a function. The method name() conflicts with the existing name property initialized in the constructor.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
let Boy = function () {
this.age = 15;
this.color = "white";
this.name = "John"
return this;
}
Boy.prototype.name = function (name) {
this.name = name;
return this;
}
let myNewBoy = new Boy();
myNewBoy.name("Harry");
</script>
</body>
</html>
Output:
TypeError: myNewBoy.name is not a function
The correct way to define the property is:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
let Boy = function () {
this.age = 15;
this.color = "white";
this.boyName = "John"
return this;
}
Boy.prototype.name = function (name) {
this.boyName = name;
return this;
}
let myNewBoy = new Boy();
myNewBoy.name("Harry");
</script>
</body>
</html>
Note: Make sure to import the module correctly. Suppose we have a 'helpers.js' file. So, we have to import the app.js:
import helpers from './helpers'
Example 5: This code causes a TypeError because 4(4 + 5) is incorrectly written. The number 4 cannot be treated as a function. This will result in an invalid function call.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Type Error</title>
</head>
<body>
<script>
const n = 4(4 + 5);
document.write(n);
</script>
</body>
</html>
Output:
TypeError: 4 is not a function
The correct way is "4*(4+5)".
Similar Reads
JavaScript TypeError - "X" is not a function
This JavaScript exception is not a function that occurs if someone trying to call a value from a function, but in reality, the value is not a function. Message: TypeError: Object doesn't support property or method {x} (Edge) TypeError: "x" is not a function Error Type: TypeError Cause of Error: Ther
1 min read
JavaScript TypeError - "X" is not a constructor
This JavaScript exception is not a constructor that occurs if the code tries to use an object or a variable as a constructor, which is not a constructor. Message: TypeError: Object doesn't support this action (Edge) TypeError: "x" is not a constructor TypeError: Math is not a constructor TypeError:
1 min read
Uncaught ReferenceError: $ is not a function
In this article, we will see the "Uncaught ReferenceError: $ is not a function". The ReferenceError is an object that represents an error that arises when the variable is not initialized or simply, variable doesn't exist in the current scope & it is referenced. The $ sign is an alias of jQuery.
2 min read
How to Fix 'TypeError: forEach is Not a Function' in JavaScript?
The error "TypeError: forEach is not a function" happens when you try to use the forEach() method on something that isn't an array or an iterable object. This usually occurs when you're working with a variable that you think is an array, but itâs not.To fix this, you just need to make sure the varia
3 min read
JavaScript TypeError - "X" is (not) "Y"
This JavaScript exception X is (not) Y occurs if there is a data type that is not expected there. Unexpected is undefined or null values. Message: TypeError: Unable to get property {x} of undefined or null reference (Edge) TypeError: "x" is (not) "y" (Firefox) Few example are given below: TypeError:
1 min read
JavaScript TypeError - 'X' is not iterable
This JavaScript exception is not iterable occurs if the value present at the right-hand-side of forâ¦of or as argument of a function such as Promise.all or TypedArray.from, can not be iterated or is not an iterable object. Message: TypeError: 'x' is not iterable (Firefox, Chrome) TypeError: 'x' is no
1 min read
JavaScript TypeError - "X" is not a non-null object
This JavaScript exception is not a non-null object that occurs if an object is not passed where it is expected. So the null is passed which is not an object and it will not work. Message: TypeError: Invalid descriptor for property {x} (Edge) TypeError: "x" is not a non-null object (Firefox) TypeErro
1 min read
JavaScript Function length property
The Javascript Function.length property of the function object in Javascript is used to return the number of parameters required by a function. Syntax: function.length Parameters: This method requires no parameters. Return: Return type is number. A few examples are given below for a better understan
2 min read
How to write a function in JavaScript ?
JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
4 min read
JavaScript TypeError - "X" has no properties
This JavaScript exception null (or undefined) has no properties that occur if there is an attempt to access properties of null and undefined. They don't have any such properties. Message: TypeError: Unable to get property {x} of undefined or null reference (Edge) TypeError: null has no properties (F
1 min read