My tests suggest that the title is, indeed, correct. But I don’t know if there is some subtle nuance that I’m not thinking of. See also: Is there anything wrong with declaring your vars inside of a for loop or an if block?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If they are in the same scope, your test is right. Redeclaring the same variable in the same scope does nothing.
But, if they are not in the same scope, re-declaring a variable in a local scope will create a new variable that will override the original within that scope.
So, this works fine:
But, this creates a new variable in the local scope that does not have the value of the globally defined one: