explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

JavaScript null vs undefined & default values

Leave a comment

[note to self]

During my recent journey into learning more about Javascript, I ran into an interesting little issue.  It seems that the default value (or lack thereof) in JavaScript is undefined, not null as it is in ActionScript.

I fired up Firebug’s console and did a little test (which by the way is a great means of learning JavaScript by means of experimentation)

Let’s start with the basic equality:

>>> foo
undefined
>>> foo == null
true
>>> foo == undefined
true
>>> null == undefined
true

And now let’s use strict equality:

>>> foo === null
false
>>> foo === undefined
true
>>> null === undefined
false

Nothing revolutionary however it does illustrate that while you can evaluate a value to null in most checks, there is that caveat that if you haven’t explicitly set a value to null (which is a value/type itself in laymen’s speak) then it is not null but rather undefined.  Doing more research I found this post by a guy who explains it a bit better than I have – http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html


					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s