简体   繁体   中英

Uncaught reference error: [variable] is not defined

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM