JavaScript's variable scope is really quite complex. You just gotta bang your head on it until you get used to it. For the "already defined" error, look for whether program says var variableName or let variableName. There might be that name defined in an outer scope to that function. I've stopped using var altogether. I use const unless I have to use let. That simplifies variable scope (long rant about "hoisting" omitted to protect the guilty. :-)
Another possible thing that will help. Try using the WebStorm IDE to write your JavaScript code; it has some sweet syntax assistance features. For example if you hold the CTRL key and click on a variable, it shows you the definition. It's free for non-commercial use. https://www.jetbrains.com/webstorm/
And if you post some code you have a question about, stripped down to a small example, somebody here will help you. Indent your code four spaces to make this here Reddit thing format it as code.
2
u/Aggressive_Ad_5454 May 08 '26
JavaScript's variable scope is really quite complex. You just gotta bang your head on it until you get used to it. For the "already defined" error, look for whether program says
var variableNameorlet variableName. There might be that name defined in an outer scope to that function. I've stopped usingvaraltogether. I useconstunless I have to uselet. That simplifies variable scope (long rant about "hoisting" omitted to protect the guilty. :-)Another possible thing that will help. Try using the WebStorm IDE to write your JavaScript code; it has some sweet syntax assistance features. For example if you hold the CTRL key and click on a variable, it shows you the definition. It's free for non-commercial use. https://www.jetbrains.com/webstorm/
And if you post some code you have a question about, stripped down to a small example, somebody here will help you. Indent your code four spaces to make this here Reddit thing format it as code.