hoisting & closure - confusion
up vote
0
down vote
favorite
Hoisting:
console.log(h)
var h = 1
This returns undefined
, since the declaration is moved to the top, but the value is assigned after the console.log()
, just like this:
var h;
console.log(h)
h = 1
This now returns 1, which is don't understand, since it's supposed to be the exact same as above in my understanding
Closure: Why can console.log()
as a function not access the global var h
in the first example?
would be really happy, if sb could help me out here.
thank you!
javascript closures hoisting
add a comment |
up vote
0
down vote
favorite
Hoisting:
console.log(h)
var h = 1
This returns undefined
, since the declaration is moved to the top, but the value is assigned after the console.log()
, just like this:
var h;
console.log(h)
h = 1
This now returns 1, which is don't understand, since it's supposed to be the exact same as above in my understanding
Closure: Why can console.log()
as a function not access the global var h
in the first example?
would be really happy, if sb could help me out here.
thank you!
javascript closures hoisting
1
This has nothing to do with closure - and your second example should indeed outputundefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering theh1 from the first snippet, which still holds the value
1`.
– Robin Zigmond
Nov 11 at 10:37
2
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
"This now returns 1" - no, it does not? Running the snippet showsundefined
as expected.
– Bergi
Nov 11 at 11:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hoisting:
console.log(h)
var h = 1
This returns undefined
, since the declaration is moved to the top, but the value is assigned after the console.log()
, just like this:
var h;
console.log(h)
h = 1
This now returns 1, which is don't understand, since it's supposed to be the exact same as above in my understanding
Closure: Why can console.log()
as a function not access the global var h
in the first example?
would be really happy, if sb could help me out here.
thank you!
javascript closures hoisting
Hoisting:
console.log(h)
var h = 1
This returns undefined
, since the declaration is moved to the top, but the value is assigned after the console.log()
, just like this:
var h;
console.log(h)
h = 1
This now returns 1, which is don't understand, since it's supposed to be the exact same as above in my understanding
Closure: Why can console.log()
as a function not access the global var h
in the first example?
would be really happy, if sb could help me out here.
thank you!
console.log(h)
var h = 1
console.log(h)
var h = 1
var h;
console.log(h)
h = 1
var h;
console.log(h)
h = 1
javascript closures hoisting
javascript closures hoisting
edited Nov 11 at 10:45
trincot
114k1477109
114k1477109
asked Nov 11 at 10:20
Susa
31
31
1
This has nothing to do with closure - and your second example should indeed outputundefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering theh1 from the first snippet, which still holds the value
1`.
– Robin Zigmond
Nov 11 at 10:37
2
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
"This now returns 1" - no, it does not? Running the snippet showsundefined
as expected.
– Bergi
Nov 11 at 11:09
add a comment |
1
This has nothing to do with closure - and your second example should indeed outputundefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering theh1 from the first snippet, which still holds the value
1`.
– Robin Zigmond
Nov 11 at 10:37
2
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
"This now returns 1" - no, it does not? Running the snippet showsundefined
as expected.
– Bergi
Nov 11 at 11:09
1
1
This has nothing to do with closure - and your second example should indeed output
undefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering the h1 from the first snippet, which still holds the value
1`.– Robin Zigmond
Nov 11 at 10:37
This has nothing to do with closure - and your second example should indeed output
undefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering the h1 from the first snippet, which still holds the value
1`.– Robin Zigmond
Nov 11 at 10:37
2
2
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
"This now returns 1" - no, it does not? Running the snippet shows
undefined
as expected.– Bergi
Nov 11 at 11:09
"This now returns 1" - no, it does not? Running the snippet shows
undefined
as expected.– Bergi
Nov 11 at 11:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Indeed those 2 cases are similar, so what's happening here ?
I think you got a bit fooled by your console. Your console always log something when you execute a command anyways. You can read this post to get better acknowledges about what get returned by your console by default when you do h=1;
and var h=1;
.
why can console.log() as a function not access the global var h in the first example? :
It actually accesses the global h
variable. But since there is not value assigned yet to h, well the console.log returns undefined
which basically means that the h
variable exists but the value it has is undefined. If it didn't find the h
variable it would have returned instead an error saying that h is not defined
which might be confusing, but is not the same as undefined
It does not access the global. The expressionh
, referencing the global, is evaluated whenlog
is called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Indeed those 2 cases are similar, so what's happening here ?
I think you got a bit fooled by your console. Your console always log something when you execute a command anyways. You can read this post to get better acknowledges about what get returned by your console by default when you do h=1;
and var h=1;
.
why can console.log() as a function not access the global var h in the first example? :
It actually accesses the global h
variable. But since there is not value assigned yet to h, well the console.log returns undefined
which basically means that the h
variable exists but the value it has is undefined. If it didn't find the h
variable it would have returned instead an error saying that h is not defined
which might be confusing, but is not the same as undefined
It does not access the global. The expressionh
, referencing the global, is evaluated whenlog
is called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05
add a comment |
up vote
0
down vote
accepted
Indeed those 2 cases are similar, so what's happening here ?
I think you got a bit fooled by your console. Your console always log something when you execute a command anyways. You can read this post to get better acknowledges about what get returned by your console by default when you do h=1;
and var h=1;
.
why can console.log() as a function not access the global var h in the first example? :
It actually accesses the global h
variable. But since there is not value assigned yet to h, well the console.log returns undefined
which basically means that the h
variable exists but the value it has is undefined. If it didn't find the h
variable it would have returned instead an error saying that h is not defined
which might be confusing, but is not the same as undefined
It does not access the global. The expressionh
, referencing the global, is evaluated whenlog
is called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Indeed those 2 cases are similar, so what's happening here ?
I think you got a bit fooled by your console. Your console always log something when you execute a command anyways. You can read this post to get better acknowledges about what get returned by your console by default when you do h=1;
and var h=1;
.
why can console.log() as a function not access the global var h in the first example? :
It actually accesses the global h
variable. But since there is not value assigned yet to h, well the console.log returns undefined
which basically means that the h
variable exists but the value it has is undefined. If it didn't find the h
variable it would have returned instead an error saying that h is not defined
which might be confusing, but is not the same as undefined
Indeed those 2 cases are similar, so what's happening here ?
I think you got a bit fooled by your console. Your console always log something when you execute a command anyways. You can read this post to get better acknowledges about what get returned by your console by default when you do h=1;
and var h=1;
.
why can console.log() as a function not access the global var h in the first example? :
It actually accesses the global h
variable. But since there is not value assigned yet to h, well the console.log returns undefined
which basically means that the h
variable exists but the value it has is undefined. If it didn't find the h
variable it would have returned instead an error saying that h is not defined
which might be confusing, but is not the same as undefined
edited Nov 11 at 10:57
answered Nov 11 at 10:38
Pierre Capo
711219
711219
It does not access the global. The expressionh
, referencing the global, is evaluated whenlog
is called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05
add a comment |
It does not access the global. The expressionh
, referencing the global, is evaluated whenlog
is called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05
It does not access the global. The expression
h
, referencing the global, is evaluated when log
is called, and the value is passed into the function by value.– ohlec
Nov 11 at 15:05
It does not access the global. The expression
h
, referencing the global, is evaluated when log
is called, and the value is passed into the function by value.– ohlec
Nov 11 at 15:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247768%2fhoisting-closure-confusion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This has nothing to do with closure - and your second example should indeed output
undefined
. Are you perhaps entering this code directly into the console, one after the other? Because if so then it is remembering theh1 from the first snippet, which still holds the value
1`.– Robin Zigmond
Nov 11 at 10:37
2
Added snippets to your question which clearly show the second script does not return 1.
– trincot
Nov 11 at 10:45
"This now returns 1" - no, it does not? Running the snippet shows
undefined
as expected.– Bergi
Nov 11 at 11:09