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: "x" is undefined
TypeError: "y" is null
TypeError: "undefined" is not an object
TypeError: "y" is not an object or null
TypeError: "x" is not a symbol

Error Type:

TypeError

Cause of Error: There is an unexpected datatype provided to any of the methods, Which expects something else. This occurs with undefined or null values.

Example 1: In this example, the variable(‘var_name’) is undefined, So the error has occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Type Error</title>
</head>
<body>
    <script>
    var var_name = undefined;
    document.write(var_name.substring(3));
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

TypeError: Unable to get property 'substring' of undefined or null reference

Example 2: In this example, the variable(‘var1’) is null, So the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Type Error</title>
</head>
<body>
    <script>
    var var1 = null;
    document.write(var1.substring(3));
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

TypeError: Unable to get property 'substring' of undefined or null reference

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.