I have a .js file that I am executing with NodeJS. Here are the contents of my file:
var ctry = "America";
function outer(msg) {
console.log(msg + " " + ctry) ;
var ctry = "Canada" ;
}
outer("God Bless");
When I run this file, I expect to see "God Bless America" but instead, I see "God Bless undefined".
If I comment out the inner var ctry = line I get "God Bless America" and if I move the inner var ctry = line above the console.log, I get "God Bless Canada". These last two cases seem logical but why does the very definition of var ctry after the console.log cause ctry to be set to underfined in the code above?