Why doesn’t var foo = foo throw a ReferenceError?
Note: foo = foo does throw a ReferenceError.
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.
When you declare
you declare the variable for the entire scope (that is your function if not global), not just the code afterwards, contrary to other languages.
So in the right part of the assignment,
foois already declared, even if it is stillundefined. There is no reference error.Note that this property of var declaration in javascript can be a source of error. Because you might very well have (in more complex) this kind of code :