Is there any purpose to redeclaring JavaScript variables?
问题 I am new to JavaScript. <html> <body> <script type="text/javascript"> var x=5; document.write(x); document.write("<br />"); var x; document.write(x); </script> </body> </html> Result is: 5 5 When x is declared the second time it should be undefined, but it keeps the previous value. Please explain whether this redeclaration has any special purpose. 回答1: You aren't really re-declaring the variable. The variable statement in JavaScript, is subject to hoisting, that means that they are evaluated