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 16. What will be the output of the following code? #31

Closed eduardomoroni closed 5 years ago

eduardomoroni commented 6 years ago

image

not exactly

image

ganqqwerty commented 6 years ago

funny! which browser did you use to get this result? Can you try it again in chrome in a new tab? It seems that you declared variable bar before somewhere?

akshayjai1 commented 6 years ago

I got the same console message. Seems like Javascript throws away the function name when function is written as expression, please find attached image screen shot 2018-06-24 at 8 29 58 pm

akshayjai1 commented 6 years ago

You can also create one more question on similar lines, i.e. what will be output of following two lines screen shot 2018-06-24 at 8 43 13 pm

ganqqwerty commented 6 years ago

Answer to your first message:

well, besically you replaced bar with y and got the same result as in the book.

Answer to your second message:

Wait a minute, your second message doesn't explain anything, check out what x is, it's just an object. It's also kinda convoluted, because you have x in the name of a variable and x in the name of a function. The new function construction is not a way to create functions, the result of a new operator will just be an empty object.

What I don' understand is why 3 is printed, hmm...

ganqqwerty commented 5 years ago

@eduardomoroni still need help from you with this question

eduardomoroni commented 5 years ago

Sorry @ganqqwerty, Just tried again: I'm using Google Chrome Version 70.0.3538.77 (Official Build) (64-bit) image

I'm getting: Uncaught ReferenceError: bar is not defined which is the expected output.