Open In App

JavaScript: Uncaught TypeError: n is not a function

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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)".


Next Article

Similar Reads