JavaScript TypeError – Can’t access property “X” of “Y”

This JavaScript exception can’t access property occurs if property access was performed on undefined or null values.

Message:

TypeError: Unable to get property {x} of undefined or 
           null reference (Edge)
TypeError: can't access property {x} of {y} (Firefox)
TypeError: {y} is undefined, can't access property {x} 
           of it (Firefox)
TypeError: {y} is null, can't access property {x} of it
           (Firefox)

Examples: 

TypeError: x is undefined, can't access property "prop" 
           of it 
TypeError: x is null, can't access property "prop" of it 
TypeError: can't access property "prop" of undefined
TypeError: can't access property "prop" of null

Error Type:

TypeError

Cause of Error: The property access was performed on any of the undefined or null values in the code.



Example 1: In this example, the GFG is undefined, So the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var GFG = undefined;
    GFG.substring(3); // error here
</script>

chevron_right


Output(in console):

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

Example 2: In this example, the GFG is null, So the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var GFG = null;
    GFG.substring(3); // error here
</script>

chevron_right


Output(in 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.