Since in JavaScript functions are same as objects, they should have a similar scope in terms of where they are defined. However, the following code confuses me:
console.log(lateFunc());
function lateFunc(){
return 'This function comes late!';
};
console.log(lateVar);
var lateVar = 'This variable comes late!';
In this case, lateFunc
and lateVar
are both define AFTER console command. However, this is what I got from node test.js
:
This function comes late!
undefined
The first console recognizes the function that is defined later. However, the second one does not recognize the variable defined later. So I'm confused about why this is happening. Shouldn't they both have global scope OR visible only after definition? Can anyone explain?