function y() {
var x = 'hi';
function x() {
return 'bye';
};
return x(); // x is not a function
return x; // 'hi'
}
console.log(y())Not able to get this function execution. Can someone explain?
Advertisement
Answer
Function and variable declarations are hoisted. Function declarations also hoist the assignment of the value.
So both function x and var x create a variable named x in the current scope. function x also assigns a function to that variable.
Assignments with = are not hoisted.
So x = 'hi' overwrites that function with a string.