In this blog post we will dig deeper into various aspects of JavaScript scope. This is a pretty interesting topic and also a topic which confuses many beginning JavaScript programmers.
Understanding JavaScript scope helps you write bug free programs (hmm.. atleast helps your troubleshoot things easily)
Scope control the visibility and lifetimes of variables and parameters. This is important from the perspective of avoiding naming collisions and provides memory management service.
Unlike other languages, JavaScript does not have block level scope. For e.g. take for instance the following piece of c# code.
If you write the same code in JavaScript, then the value of 'b' will be available outside the 'if' block. The reason for this is JavaScript does not have block level scope. Scopes works at the function level and because of a feature called 'hoisting' all variable declaration in JavaScript is moved to the top of the function.
You can read more about hoisting here http://rajeshpillai.tekacademy.com/hoisting/
Happy Scoping !!!
Understanding JavaScript scope helps you write bug free programs (hmm.. atleast helps your troubleshoot things easily)
Scope control the visibility and lifetimes of variables and parameters. This is important from the perspective of avoiding naming collisions and provides memory management service.
Unlike other languages, JavaScript does not have block level scope. For e.g. take for instance the following piece of c# code.
public void Main () {
int a = 5;
if (true) {
int b = 10;
}
// This will throw compile time error as b is not defined
// and not within the scope of function Main();
Console.WriteLine(b);
}
If you write the same code in JavaScript, then the value of 'b' will be available outside the 'if' block. The reason for this is JavaScript does not have block level scope. Scopes works at the function level and because of a feature called 'hoisting' all variable declaration in JavaScript is moved to the top of the function.
You can read more about hoisting here http://rajeshpillai.tekacademy.com/hoisting/
function scopeDemo() {
var a = 5;
if (true) {
var b = 10;
}
// This will work even though 'b' is defined inside if block because of 'hoisting'
alert(b);
}
scopeDemo(); // Invoke the function
Remember, Scope in JavaScript is at function level and not block level.
So, contrary to popular guidelines of most programming lanaguage to declare variables as late as possible, in JavaScript it is recommended to declare all your variables as early as possible.
Happy Scoping !!!
You can try out the demo here
Comments
because i enjoy to gain knowledge of more and more.
http://shop.xaijo.com/?new-qz.html
behalf executive topics physical feeding discovery averages kerr fosamaxd authenticity brief spite pursuant anafranil inflammation suite zostrix boxes sheet stanley headaches coconut protozoa fagerstrom flows
http://intimcity.org/in.htm?profile-wl&wm=2020890811
I really loved reading your post. Thanks!A field nearby to my nerve cheers, do you have a RSS graze ?...
I can see you have high standards when it comes to your writing. It shows in your excellent writing. Thank you for your high standards....
In this awesome pattern of things you actually secure a B- with regard to effort and hard work. Exactly where you actually confused me was first in your facts. As as the maxim goes, the devil is in the details... And that could not be more accurate her...
I do not usually find this type of data interesting, but you have proven that a talented writer can take information that is often considered dull and make it pop....
I can see you have high standards when it comes to your writing. It shows in your excellent writing. Thank you for your high standards....
I do not usually find this type of data interesting, but you have proven that a talented writer can take information that is often considered dull and make it pop....
My spouse and I stumbled over here from a different page and thought I might as well check things out. I like what I see so i am just following you. Look forward to going over your webpage repeatedly....
http://shop.xaijo.com/?new-wo.html
inefficiency unreliable demo tveyes burn asserted ceftin orderpharma epsos container suits booklet unimportant naspa responsive until acquired going submissions codenob strives network lucarotti scholarship eprescribing
http://intimcity.org/in.htm?profile-oc&wm=2020890811
http://search.erolove.in/?new-rb.html
http://blog.erolove.in/land?browse-em.html
you write. The arena hopes for more passionate writers such as you who
aren't afraid to say how they believe. At all times go after your heart.