Home > database >  How to solve "Cannot redeclare block-scoped variable" problem in javascript?
How to solve "Cannot redeclare block-scoped variable" problem in javascript?

Time:09-04

Cannot redeclare block-scoped variable

Problem is occurring with variable called "libraryForm"

CodePudding user response:

To fix it don't declare the same variable in the same block.

You can't have the same variable name in the same block.

CodePudding user response:

It looks like you are assigning libraryForm multiple times (most likely at the global scope)

Based on the limited nature of your screen shot, it seems like the solution to your problem would be to only assign libraryForm variable once to the proper HTML element in your code

Your solution would consist of :

  • getting rid of the unused assignment of libraryForm on line 18
  • Leaving the single assignment of libraryForm = document.getElementById('libraryform') on line 22
  • getting rid of the duplicate assignment later down in your code.

It seems like you should learn more about the difference of let vs var, block scope, and variable hoisting.

https://www.w3schools.com/JS/js_let.asp

  • Related