0

I have the following variable declared at the top level of the script:

var region = “southwest”;

When running

console.log(region)

I'm getting the following error:

VM1094:1 Uncaught ReferenceError: region is not defined at :1:13

Not sure what is causing this given the variable is global.

  • Is the script actually a module? – Teemu Apr 08 '22 at 04:57
  • Yes, it’s a module. I set it up that way to use async funcions from other modules. – user3552172 Apr 09 '22 at 20:13
  • Modules create a "private" namespace, when you declare a variable in the body of a module, it's not global, it's local to the namespace the module is bound to. See also https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Teemu Apr 10 '22 at 06:58

2 Answers2

0

when you have global variables they will be assigned to window object , in your console try console.log(window.region)

  • I tried this and now it’s showing undefined, but the variable is defined. I tried this with other variables that clearly have a value and for some reason got the same result. – user3552172 Apr 09 '22 at 20:17
  • I know it's not good but a picture of your code would be helpfull – hossein samani Apr 17 '22 at 14:41
-1

Is "southwest" a string or a variable that you have assigned to the 'region' variable? If it's a string, you need to put it inside the "double quotes". And, if it is a variable, then you need to assign some value to it.

Sandesh GC
  • 26
  • 4
  • Add request for clarification or question in a comment under the question, answer only when you understand the question or the person asking has edited their question for clarity. – Jan Bussieck Apr 08 '22 at 08:08
  • southwest is the value assigned to the variable, for some reason when I pasted here it dropped the “” enclosure, but it does have it in my code. – user3552172 Apr 09 '22 at 20:15
  • Could you add the whole code that you're trying to run? Because in these two statements there is no problem and it runs correctly, so I cannot help you. – Sandesh GC Apr 11 '22 at 15:34