Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch 4 Functions First is a little confusing #63

Closed
davidbkemp opened this issue Feb 15, 2014 · 5 comments
Closed

Ch 4 Functions First is a little confusing #63

davidbkemp opened this issue Feb 15, 2014 · 5 comments

Comments

@davidbkemp
Copy link

You state that functions are hoisted before variables, and yet these functions can refer to variables defined in the same scope. Is it simply the case that the variables will be defined by the time the function is actually called, or is it that all the variable and function names are hoisted together, then the functions defined, and then the remainder of the code executed?

@getify
Copy link
Owner

getify commented Feb 16, 2014

I think the proper way to think about it is: functions are hoisted first and then variables, but a function execution (as with any other sort of statement) always happens after both have been declared, which means that a function referencing a variable will find it by the time the function is executed.

@davidbkemp
Copy link
Author

OK, I guess it is a fairly minor distinction anyway. Keep up the great work!

@getify
Copy link
Owner

getify commented Feb 16, 2014

Thanks! :)

@muradsofiyev
Copy link

function foo() {
    console.log( foo );
}
var foo;


foo()

But what is happened in there? If functions are hoisted first and then variables we can't call foo like function but we can

@getify
Copy link
Owner

getify commented Aug 17, 2019

var foo; does not do the same thing as var foo = undefined;. The former creates a variable that is auto-initialized at the top of the scope. The latter has as assigment expression that happens during runtime at that point in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants