Skip to content
Advertisement

Hoisting inside function – inner function and variable having same name – Output?

JavaScript

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement