Home > Net >  Uncaught reference error: [variable] is not defined
Uncaught reference error: [variable] is not defined

Time:09-30

I am a beginner, just learning JavaScript and have created an object (in VS Code) to practice editing properties etc.

However, when I try to run the script in the browser console it says the object is not defined - which I thought I had done with the 'const' statement.

Probably I've missed a step somewhere - surely something really simple: can anyone help, please?

I'm loading the JS file (called script.js) from an HTML file, which looks like this: [enter image description here][3]

I'm trying to run the JS by pressing 'Go live' in VS Code and then accessing the browser console - but then get the error that Chrome doesn't see my object.

CodePudding user response:

That is pretty straightforward. I think your JavaScript isn't being executed. I'm guessing, but it looks like this is contained in a JS file. So you're probably loading it in from an HTML file.

If that's correct, check the tag that loads it.

You might want to add a line to your JavaScript that lets you know in the console the JS was loaded. Perhaps: console.info("Drumkit JavaScript loaded"); after line 21.

CodePudding user response:

Yay, I fixed it! I moved all the files (in MS Explorer) into a sub-directory and then created a new workspace (in VS Code) which pointed to the top folder in that directory. I could then (in the browser) just click on this one JS file - and it executed correctly!

I also added the console.info line that you suggested, to confirm that the JS loaded correctly - many thanks for the tip.

  • Related