11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note Some Netscape browsers do not support this property so they will always display the<br />

second message (―Persistent cookies are not enabled‖). This is because Boolean<br />

variables are assigned a default value of false if none is assigned.<br />

Undefined and Null<br />

<strong>The</strong> undefined type is used for variables or object properties that either do not exist or have not<br />

been assigned a value. <strong>The</strong> only value an undefined type can have is undefined. For example,<br />

declaring a variable without assigning it a value,<br />

var x;<br />

gives x the undefined type and value. Accessing a nonexistent object property,<br />

var x = String.noSuchProperty;<br />

also results in the assignment of undefined to x.<br />

<strong>The</strong> null value indicates an empty value; it is essentially a placeholder that represents<br />

―nothing.‖ <strong>The</strong> distinction between undefined and null values is tricky. In short, undefined<br />

means the value hasn‘t been set, whereas null means the value has been set to be empty.<br />

Why on earth would the designers of <strong>JavaScript</strong> permit such a confusing distinction? Without<br />

getting into the arcane details, there are actually certain cases where it can be useful,<br />

particularly if you‘re using the object-oriented features of the language. For example, suppose<br />

you‘re writing an object whose functionality for a method called doFancyStuff() depends upon<br />

a feature offered only by some browsers. You might do some browser detection and define the<br />

function doFancyStuff() appropriately if the browser is one that supports the required feature.<br />

But if the user has an unrecognized browser, you might set doFancyStuff to null to indicate<br />

the method is unavailable. In this way, you can distinguish between the case that the feature is<br />

supported (doFancyStuff is a function), the case that the feature isn‘t supported<br />

(doFancyStuff is null), and the case that the browser detection code hasn‘t been run<br />

(doFancyStuff is undefined).<br />

<strong>The</strong>re is one further wrinkle to be aware of: the null value is defined as an empty object.<br />

Because of this, using the typeof operator on a variable holding null shows its type to be<br />

object. In comparison, the type of undefined data is undefined.<br />

Distinguishing Between Null and Undefined Data<br />

<strong>JavaScript</strong> provides the null keyword to enable comparison and assignment of null values.<br />

Unfortunately, the undefined keyword exists only in modern browsers (Netscape 6+ and<br />

Internet Explorer 5.5+). <strong>The</strong> resolution lies in the fact that null and undefined values compare<br />

equal. So you can check for invalid values by comparing to null. For example, given the<br />

declarations,<br />

var x;<br />

var y = null;<br />

the following comparisons are true:<br />

if (x == null)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!