Thursday, October 23, 2025
HomeLanguagesJavascriptUncaught ReferenceError: controller is not defined

Uncaught ReferenceError: controller is not defined

Uncaught ReferenceError: the controller is not defined is a common error message that can occur in JavaScript. It usually occurs when you try to access a variable or function that has not been defined or is out of scope.

An example where this error occurs: For example, the following code will result in an Uncaught ReferenceError: the controller is not defined error because the variable “controller” has not been declared.

Javascript




<script>
    function myFunction() {
        console.log(controller);
    }
  
    myFunction();
</script>


Output:

Uncaught ReferenceError: controller is not defined

Approach: The solution to this error is to make sure that all variables are declared before they are used. In the example above, the code can be modified to declare the “controller” variable before using it.

Example 1: Below is the code illustration of the above approach:

Javascript




<script>
    let controller = "Hello, world!";
  
    function myFunction() {
        console.log(controller);
    }
  
    myFunction();
</script>


Output:

Hello, world!

Example 2: Use the let keyword to declare the variable within a block of code

Javascript




<script>
      function myFunction() {
          let controller = "Hello, world!";
          console.log(controller);
      }
  
      myFunction();
</script>


Output:

Hello, world!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS