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

Introduction

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: labreuer+javascript@gmail.com .  Please note that this page is dedicated to client-side javascript.

References
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 1.2.3.4, 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: 'http://luke.breuer.com', 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 = 'http://luke.breuer.com'; 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 }
Debug