JavaScript ReferenceError – Variable is not Defined

Working with a programming language often causes one to encounter errors frequently, but knowing how to find the error and how to fix the error is nothing less than skill. JavaScript Reference error is quite a common error that people encounter (especially beginners).

This error simply means that there exists such a line in the code that is telling the compiler to access a variable or an object that has no memory address or location. Such a scenario happens when the variable in focus here is not yet initialized or declared at all.

If the variable is not declared, then it will not take up a memory location or address. By this statement, it is easy to conclude that this “ReferenceError – Variable is not Defined” occurs when the variable that the programmer is trying to access has not been previously declared prior to the statement that caused the error.

Error Message of “ReferenceError – Variable is not Defined”

The error message of the reference message, at first glance, seems very daunting because it looks something like this:

The error message contains around 10 different lines, and all of these lines tell a different story on why the error was caused. Now, if the programmer were to visit the files linked in these 10 lines and try to figure out the error, he would find himself in a maze.

Breaking Down the Error Message of “ReferenceError – Variable is not Defined”

Take a look at the following screenshot of the same error message that was used in the previous section:

Let’s explain the markings that are done on the screenshot:

  • 1: This is the JavaScript statement that has caused the error
  • 2: This is the variable whose reference the compiler couldn’t find
  • 3: The file name and the line number of the statement which caused the error
  • 4: Files of the environment that led to error (Ignore these lines)

Fixing the “JavaScript ReferenceError – Variable is not Defined”

Fixing this error is quite simple, go to the line that has been mentioned in the error message and use a variable name that has been declared prior to that statement. To demonstrate this, take the following code:

functionaddNums(num1, num2) {
returnnum1 + num2;
result = addNums(5 , 6);


The above lines do the following:

  • Create a function addNums which returns the sum of two number passed inside its arguments
  • Use the function to calculate the sum of 5 and 6, and store the return value in the result variable
  • Print the result using the results variable

Executing the code produces the following error message:

It says the “results” variable couldn’t be referenced and points to the line number 6. Now, compare line 6 and line 4:

result = addNums(5 + 6); // Line Number 4

console.log(results); // Line number 6

It is clear that the error is caused due to the misspelling of the name of the variable in line number 6. Correct the spelling of the identifier which has the return value of the function to:

result = addNums(5 + 6); // Line Number 4

console.log(result); // Line number 6

After that, execute the program and observe the following output:

The output shows that the program is now working without any errors


The JavaScript ReferenceError – Variable is not Defined is trying to access a variable using its identifier which has not been declared prior to that statement. This can be caused by misspelling or by simply missing a whole statement in which the programmer was supposed to declare the variable. The way to fix this method is to go to the line number mentioned in the error message and fix the name of the variable or declare the variable prior to that statement.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.