1st function call “howDoIHoist(true)” will print :-
What happens here? Undefined
And prints condition in “if” statement
This is because inside the scope of function “howDoIHoist()”,
“whatHappensWhenIGetHoisted” is variable and is used before it is defined. Hence,it will give “undefined”. According to the hoisting of variables which I’ve covered in this blog.
Moreover,“scope of a function” means the scope defined by the function's body, in which its local variables are declared
Variable “whatHappensWhenIGetHoisted” is inside a function so it is in the scope of that function. We can’t say here that variable “whatHappensWhenIGetHoisted” is in different scope if it used in if-else expression.
You can read more about Closure in my blog Link,here,I have covered this topic,it might be helpful for you!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey alright, variables in functions also get hoisted.
What happens when the same variable is defined in different scopes in the same function and they both get hoisted?
Thanks for the post!
1st function call “howDoIHoist(true)” will print :-
What happens here? Undefined
And prints condition in “if” statement
This is because inside the scope of function “howDoIHoist()”,
“whatHappensWhenIGetHoisted” is variable and is used before it is defined. Hence,it will give “undefined”. According to the hoisting of variables which I’ve covered in this blog.
Moreover,“scope of a function” means the scope defined by the function's body, in which its local variables are declared
Variable “whatHappensWhenIGetHoisted” is inside a function so it is in the scope of that function. We can’t say here that variable “whatHappensWhenIGetHoisted” is in different scope if it used in if-else expression.
You can read more about Closure in my blog Link,here,I have covered this topic,it might be helpful for you!