Skip to content
Related Articles

Related Articles

Improve Article

JavaScript ReferenceError – variable is not defined

  • Last Updated : 31 Jul, 2020
Geek Week

This JavaScript exception variable is not defined occurs if there is a non-existent variable that is referenced somewhere.


ReferenceError: "x" is not defined

Error Type:


Cause of Error:

There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared, or make sure the variable is available in the current script or scope.

Example 1: In this example, the variable(val1) is being accessed from the outside of the function, So the error has not occurred.


    function sum() { 
        var val1 = 2; 
        var val2 = 3;
      return val1 + val2;


ReferenceError: 'val1' is not defined

Example 2: In this example, the variable(GFG) is not defined, So the error has occurred.




ReferenceError: 'GFG' is not defined

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :