File size

File size

File size

File size

File size

43.5 MB

One often misunderstood topic that leads to errors in programs is how JavaScript treats variables declared inside and outside of functions. This video explains the ramifications of variable scope at both the functional and global levels. For developers coming from other programming languages, it demonstrates how your familiarity with block scope will lead you to erroneous conclusions about JavaScript's functional and global scope. Also briefly discussed is the notion of hoisting in JavaScript.

You Say about the myScope variable that you defined outside theanonymous function it's the same variable that you defined inside anonymousfunction and the value of variable is temporary change.but this is not true because the two variable have the different memory location not the samebecause in anonymous function you add the keyword var before the myScopevariable.and if you want to be the two become the same memory location you need toremove the keyword var in anonymous function.

Bob, I am enjoying these videos and learning much from them. Thank you for producing them!

However, in this video I must say I was left somewhat confused. That is, I thought there should have been more in-depth explanation for the various cases and why the results are what they are.

I too am confused by the results of the code posted by @asok14215. And while it MAY be true (don't know) that hoisting is responsible for the results (as suggested by @tssutha), I didn't get enough from this video to understand IF it's due to hoisting or WHY (if it even is). The example in the video @ 7:20 (ish) does not seem to me to be the same thing @asok14215 is doing.

Bob,Thanks for the very prompt response and link - I think I understand the hoisting concept now. You didn't add any confusion - I was already there... Don't know that nettuts explained it differently than you did, just a bit more thoroughly perhaps? As a side note, that site has a TON of information and appears to be an excellent resource (that I will now have to figure out a way to devote some time to!).

I THINK I now understand what is happening behind the scenes in asok14215's code sample...the declaration (not the assignment) portion of: var myScope = "function";is hoisted to the beginning of the function block, then its value is set to 'changed', then its value is set to 'function', then the 1st alert() is executed, thereby displaying 'function' as the value. After that, the function block ends so the second alert when executed displays the globally-scoped value for myScope, which is still 'global'.

In any case, it seems that declaring multiple variables with the same name - even if JavaScript permits it - is not a good idea and not a 'best practice.'

Just to clarify - I didn't mean to suggest that YOU were saying declaring variables with the same name IS a good idea...I was simply offering my opinion on the subject.

I guess I just don't see a good reason for doing that...perhaps there are reasons why you might have to (that I just haven't come across yet). Along the same lines, it seems to me it would be clearer and less risky to just declare the variables at the beginning of the block in which they are used, thereby eliminating hoisting issues altogether.

Remove this comment

Remove this thread

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums, or
Contact Us and let us know.