JavaScript: Scratch Page(last updated: 02/02/06 01:38 UTC)
navigate backback  


Please note that this tutorial will look best in a standards-compliant browser, such as Firefox .

I agree with Douglas Crockford's analysis that JavaScript is the world's most misunderstood programming language.  This page is intended to rectify common misconceptions about JavaScript and reveal its true, often lost capabilities.  As always, please e-mail me with any comments: .  Please note that this page is dedicated to client-side javascript.

Misconceptions about JavaScript

All of these statements are incorrect:

Browser Compatibility

It used to be that Internet Explorer implemented JScript, Netscape implemented JavaScript, and the two had very limited overlap.  Fortunately, the situation has changed; browser sniffing is out and object sniffing is in.  For a long, in-depth discussion, see QuirksMode ; the idea is that you follow the below example:

if (document.images != undefined) { // have fun with the images } else { // sulk }

Here, we don't test to see if the browser is IE, or Netscape 3.3.221, we simply ask the question, "Does this property/function/whatever exist?"  This way, we don't care about the identity of the browser, we just care about what functionality the browser supports.  If the next version of some browser supports something it didn't before, no well-written scripts will need changing.  Note that the != undefined is not necessary, but it makes it clear that we are testing for existence of an object member and not a boolean value.

The struct equivalent

JavaScript doesn't do structs, but we can come pretty close:

var author = { Name: 'Luke Breuer', Url: '', Age: 21 }; alert(author.Name)

The following is equivalent, but slightly more dangerous due to the

var author = new Object(); author.Name = 'Luke Breuer'; author.Url = ''; author.Age = 21; alert(author.Name);
null and undefined

Strictly speaking, a variable is null if it exists but has no value, whereas "something" is null if it's not even a variable.  More will be added later; see these notes in the time being.

Funkiness with null and ||

The boolean logical or operator, ||, apparently is just for boolean values in javascript.  It simply returns the first value that is not null, otherwise ???.  (Perhaps it returns false, perhaps null, perhaps undefined.)  A particularly common example is setting the event object to be the object that was passed if an object were passed, or window.event, which is what IE uses.

function Foo(e) { //e = e != null ? e : window.event; // old way e = e || window.event; // new way }