Global Properties

18 May 2010 | By Alex Young | Tags language

In When is a global variable not a variable?, Andrew Hedges discusses the difference between variables and global properties.

Try running this example:

try {
   // The alert will display undefined
   alert(a);
}
catch (e) {
   // Not executed
   alert(e);
}

var a = 10;

try {
   alert(b);
}
catch (e) {
    // The alert will display a reference error
   alert(e);
}

b = 10;

Removing var actually creates a global property. The distinction is slight, but worth being aware of:

The biggest difference between the two is that variables can’t be deleted using the delete statement. Properties can.

Most people can work safely without ever being aware of this underlying behaviour, but it’s an interesting point.


blog comments powered by Disqus