JavaScript
x
11
11
1
function y() {
2
var x = 'hi';
3
4
function x() {
5
return 'bye';
6
};
7
return x(); // x is not a function
8
return x; // 'hi'
9
}
10
11
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.