I've been curious about hoisting. I understand that if a global function shares the same name as a global variable, the function will overwrite the variable's name. Is this correct?
Here is an example code snippet.
(function() {
console.log('console.log#1 ' + globalString); // globalString function
})();
var globalString = 'I\'m globalString variable';
(function() {
console.log('console.log#2 ' + globalString); // string
})();
function globalString() {
console.log('I\'m globalString function');
}
The output of this code looks like this:
console.log#1 function globalString ()
{
console.log ( 'I\'m globalString function' );
}
console.log#2 I'm globalString variable
If a function definition overrides a variable, then console.log#2 prints the globalString function. I am unsure about how variables and functions are hoisted. Please provide some guidance.