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 (Firefox)
TypeError: undefined has no properties (Firefox)

Error Type:

TypeError

Cause of Error: Somewhere, there is access to properties of null or undefined.

Example 1: In this example, the variable(‘GFG’) is assigned the null value and It doesn’t have any property, 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 GFG = null;
    document.write(GFG.prop_name);
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

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

Example 2: In this example, the variable(‘var_name’) is assigned the undefined value and It don’t have any property, 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.prop_name);
    </script>
</body>
</html>

chevron_right


Output(In Edge console):

TypeError: Unable to get property 'prop_name' 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.