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!










share|improve this question




















  • 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








  • 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















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!










share|improve this question




















  • 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








  • 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













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




    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














  • 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








  • 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








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












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






share|improve this answer























  • It does not access the global. The expression h, referencing the global, is evaluated when logis called, and the value is passed into the function by value.
    – ohlec
    Nov 11 at 15:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer























  • It does not access the global. The expression h, referencing the global, is evaluated when logis called, and the value is passed into the function by value.
    – ohlec
    Nov 11 at 15:05















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






share|improve this answer























  • It does not access the global. The expression h, referencing the global, is evaluated when logis called, and the value is passed into the function by value.
    – ohlec
    Nov 11 at 15:05













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 10:57

























answered Nov 11 at 10:38









Pierre Capo

711219




711219












  • It does not access the global. The expression h, referencing the global, is evaluated when logis 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 logis 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 logis 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 logis called, and the value is passed into the function by value.
– ohlec
Nov 11 at 15:05


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation