ganqqwerty / 123-Essential-JavaScript-Interview-Questions

JavaScript interview Questions
BSD 3-Clause "New" or "Revised" License
5.01k stars 1.18k forks source link

Question 2 answer is incorrect #42

Closed yairEO closed 6 years ago

yairEO commented 6 years ago
var y = 1;
if (function f() {}) {
  y += typeof f;
}
console.log(y); // 1undefined

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 the if statement itself, and the JS engine does NOT create a reference for it at the beginning of the scope.

For example:

(function(){
  console.log(typeof f, typeof y) // function undefined
  function f(){}
  var y = 1;
})()

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:

(function(){
  var f = function f(){}, // not really, but "f" acts like a variable because this is only a reference to the named function
       y;  
  console.log(typeof f, typeof y); // function undefined
  y = 1;
})()

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 the if statement can handle, which is fundamental true/false scenario and then the was never really created and is discarded by GC.

Another interesting thing would be to print this:

console.log( function f(){}, (function f(){})() );  // ƒ f(){} undefined

and this also shoes that when functions are created inside specific "things" no reference is really created:

console.log( function f(){}, typeof f); // ƒ f(){} "undefined"

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.

ravikishorethella commented 6 years ago

The output of the program returns 1object

ganqqwerty commented 6 years ago

in which environment @ravikishorethella?

ravikishorethella commented 6 years ago

@ganqqwerty It return 1undefined. Sorry for the confusion

yairEO commented 6 years ago

Why this has been closed without changes being made to the README, or at least a respectful response from the repo's creator/maintainer?

ganqqwerty commented 6 years ago

Sorry, my fault, I forgot to read your, @yairEO message and only read @ravikishorethella last message.

ganqqwerty commented 6 years ago

@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.

ganqqwerty commented 6 years ago

replaced the question.