Skip to content
Related Articles

Related Articles

Improve Article
JavaScript TypeError – “X” is (not) “Y”
  • Last Updated : 24 Jul, 2020

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




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

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




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

Output(In Edge console):

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :