Closed yairEO closed 6 years ago
The output of the program returns 1object
in which environment @ravikishorethella?
@ganqqwerty It return 1undefined. Sorry for the confusion
Why this has been closed without changes being made to the README, or at least a respectful response from the repo's creator/maintainer?
Sorry, my fault, I forgot to read your, @yairEO message and only read @ravikishorethella last message.
@yairEO I tend to agree with you. This question is far too nitty-gritty, and I never had it asked in Europe or Russia. I will ask Nishant if it's popular in India. If it is popular there, I will leave it in a book and correct the explanation. If it's not asked anymore, I will replace it with one of the questions from flashcards.js that people ask in Europe and the US all the time.
replaced the question.
I do not agree with this answer as it does not explain what's going on here.
f
is a named function, but it is "created" within theif
statement itself, and the JS engine does NOT create a reference for it at the beginning of the scope.For example:
As you can see the function is declared after the printing of the
typeof f
and this works because all variables and functions references are compiled to the beginning of the scope.The above code is converted to obey the rules of JS engines:
but in the question, the function was created within the
if
statement, and the JS engine therefor does not hoist the reference to the beginning of the scope, but instead the whole thing evaluates into something theif
statement can handle, which is fundamentaltrue/false
scenario and then the was never really created and is discarded by GC.Another interesting thing would be to print this:
and this also shoes that when functions are created inside specific "things" no reference is really created:
Anyway, I think this question should never be asked in an interview because this is far far from real-life scenarios and does not show any skills beside general knowledge of lesser-known specific hoisting-related scenarios.