What they don’t teach you in (W3)school: JavaScript variable scoping

W3Fools is an interesting site I came across that highlights the wrong parts of W3Schools. While W3Schools is the most popular online resource for web standards, its content has a lot of serious flaws.

One such problem at W3Schools is variable scoping in JavaScript(w3schools page):

JavaScript has a global scope(global window object to be more precise), that is the place where all global variables go, usage of this must be minimized because in most pages there will probably be scripts from multiple sources, if everyone relies on the global scope, than sooner or later one script will get into conflict with another. The var keyword is not an optional decoration in JavaScript, if we leave out the var, then the global scope is assumed automatically. Here are some examples:

These functions declared global variables, therefore our local variable loses its original value:

something = 'first thing';
 
function foo() {
    something = 'foo';
}
function bar() {
    something = 'bar';
}
 
foo();
bar();
console.log(something); //'bar'

These functions declared local variables, no collisions have occurred here:

var something = 'first thing';
 
function foo() {
    var something = 'foo';
}
function bar() {
    var something = 'bar';
}
 
foo();
bar();
console.log(something); //'first thing'

W3Schools states the following too:

If you redeclare a JavaScript variable, it will not lose its original value.

While it may not cause an error now it is certainly not a good practice and could lead to confusion and maybe runtime errors in future versions of ECMA Script(JavaScript).

There are also other important facts about the var keyword that are not mentioned in W3Schools:

var is function scoped and not block scoped:

function test() {
    if (true) {
         var test = 3;
    }
}

works just like:

function test() {
    var test;
    if (true) {
         test = 3;
    }
}

Either way you will be able to access the variable test after the block. A better example might be:

for (var i=0;i<10;i++) {
}
console.log(i); //10

The best practice to avoid confusion is to begin each function with a single var declaration and declare all variables in that single var declaration. JSLint can help you with that. And learning more about JavaScript is as easy as watching a couple of videos I outlined in one of my previous posts.

Update:
the best comment this article/issue received so far: http://www.reddit.com/r/web_design/comments/fa9hp/what_they_dont_teach_you_in_w3school_javascript/c1ejet7

Update 2: made the w3schools link weaker.

Update 3: added rel=”nofollow” to w3schools links.

Tags: ,

  • Pingback: István Miklós Antal

  • Pingback: m.y.ikegami_bot

  • Pingback: Peter Grond

  • Pingback: What they don’t teach you in (W3)school: JavaScript variable scoping « Interesting Tech

  • http://panoptikos.com/ Panoptikos Developer

    Could you please remove the links to W3Schools? W3Fools’s mission, with which I sympathize, is to get rid of W3Schools because their documentation is rather incomplete and sometimes wrong, like you noticed. The more links point to them, the longer they stay number one. Let’s change that.

  • Pingback: Thought Provoking

  • Pingback: Charles Allhands

  • Pingback: 奥山実

  • yeah

    Problems:

    1. “it’s content has a lot of serious flaws.” should be “its content”
    2. “probably be scripts from multiply sources” should be “probably be scripts from multiple sources”
    3. then you misused “it’s” again later
    4. “Here is are some examples” should be “Here are some examples”
    and more …

    Sorry, but it’s difficult to take your corrections seriously when you have so many of your mistakes.

  • Pingback: Javascript Simple Digital Time « Kvijayanand's Blog

  • István Miklós Antal

    Fixed, fixed, fixed, fixed.

    If the W3Schools editors would be this fast, I wouldn’t have written this post in the first place.

    I did not imply that I do not make mistakes, also the W3C has content errors, I have grammatical errors, not the same.

  • István Miklós Antal

    Maybe they will listen and correct the errors, it wouldn’t be that bad. I don’t really agree in bringing them down unless of course if there is a better alternative.

  • Pingback: Scott Radcliff

  • Pingback: MunichJS

  • joe

    These are just typos, not technical problems. No need to be so picky.

    István, good write up.

  • http://www.randomthink.net/ Brian Arnold

    I would agree with the Panoptikos developer.

    You can link to w3schools with the word “w3schools” and that’s fine, but by linking with the phrase “variable scoping in JavaScript” – you’re helping w3schools show up higher when someone searches for that phrase on Google.

    You could include the URL as a non-link so that people can reference the original content without helping out their strength in search. :)

  • Anonymous

    Ok, you convinced me, modified it. Thanks for the feedback and the idea.

  • http://www.impressivewebs.com Lazaris

    Hey, Istvan. Great article, and glad you pointed out that error.

    I agree with the above comments about the link. Although you said you made it “weaker”, this is not really going to do much. You can still have a link but give the link a rel=”nofollow” attribute. This will still make the inbound link visible to W3Schools, but will give them zero link juice in terms of SEO.

    You’ll notice, on the W3Fools site, they have tons of back-links to W3Schools, and all of them are tagged with “nofollow”, to prevent Google from giving them any more credit.

    Anyways, it’s your site, so your choice. But thanks for posting this. And don’t worry about your English grammar, that’s the least important thing. :)

  • Anonymous

    Hey Lazaris,

    thanks for the suggestion, I have done it, I have no idea how this managed to slip my mind, shame on me.

    Let’s hope Google updates this soon. :)