Is there any purpose to redeclaring JavaScript variables?

前端 未结 9 1782
傲寒
傲寒 2020-12-11 18:38

I am new to JavaScript.





        
9条回答
  •  暖寄归人
    2020-12-11 19:04

    You should never redeclare a variable within the same scope, if you really want to change this then assign to it. Redeclaration is not required to create a different object in this dynamic language, if you want x to be a string just assign:

    x = "hello";
    

    It is not required that you set it back to undefined or redeclare first.

    Please note that changing the variable type is not very good practice in most situations, simply stating that it is a possibility if that's what you require.

提交回复
热议问题