Thursday, March 15, 2007
“undefined” vs. undefined
I had this bit of JavaScript code (never mind that I am compelled to add a semicolon to the end of each line when it isn't needed):
var lines = req.responseText.split("\n") for (var i = 0 ; i < lines.length ; i++) { var row = table.insertRow(-1) var fields = lines[i].split("\t") var cell; cell = row.insertCell(0) cell.textContent = fields[0] cell = row.insertCell(1) cell.textContent = fields[1] cell = row.insertCell(2) cell.textContent = fields[2] }
I have a text file which I've just loaded. I break it into lines, and then each line into sub-fields, each separated by a tab (this is the format I keep my IP allocation files in), and then stuff the entire thing into a large table.
So, from a text file that looks like this (where I'm showing the actual tab and newline characters as arrows):
10.0.0.0→00:00:00:00:00:00→Backup network↵ 10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵ 10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵ 10.0.0.3↵ 10.0.0.4↵ 10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵ 10.0.0.6↵
and I end up with something that looks like:
IP | MAC | Notes |
---|---|---|
10.0.0.0 | 00:00:00:00:00:00 | Backup network |
10.0.0.1 | DE:CA:FB:AD:00:01 | mobybackup |
10.0.0.2 | DE:CA:FB:AD:00:02 | dnsserver |
10.0.0.3 | undefined | undefined |
10.0.0.4 | undefined | undefined |
10.0.0.5 | DE:CA:FB:AD:00:03 | mailserver |
10.0.0.6 | undefined | undefined |
The undefines pop out because there's nothing else to the line except an
IP address, so fields[1]
and fields[2]
literally
have nothing in them, not even the empty string. They are devoid of any
value whatsoever.
Okay, fine, whatever.
Now, I go save what I have in the table back to the server. So I write some code:
var table = document.getElementById('tdisplay') var textfile = "" for (var i = 0 ; i < table.rows.length ; i++) { var ip = table.rows[i].cells[0].textContent var mac = table.rows[i].cells[1].textContent var note = table.rows[i].cells[2].textContent textfile = textfile + ip + "\t" + mac + "\t" + note + "\n" }
And what do I get back?
10.0.0.0→00:00:00:00:00:00→Backup network↵ 10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵ 10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵ 10.0.0.3→undefined→undefined↵ 10.0.0.4→undefined→undefined↵ 10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵ 10.0.0.6→undefined→undefined↵
Oh wait! That's not what I wanted! Okay …
for (var i = 0 ; i < table.rows.length ; i++) { var ip = table.rows[i].cells[0].textContent var mac = table.rows[i].cells[1].textContent var note = table.rows[i].cells[2].textContent if (!ip) { ip = "" } if (!mac) { mac = "" } if (!note) { note = "" } textfile = textfile + ip + "\t" + mac + "\t" + note + "\n" }
And now let's see what I get:
10.0.0.0→00:00:00:00:00:00→Backup network↵ 10.0.0.1→DE:CA:FB:AD:00:01→mobybackup↵ 10.0.0.2→DE:CA:FB:AD:00:02→dnsserver↵ 10.0.0.3→undefined→undefined↵ 10.0.0.4→undefined→undefined↵ 10.0.0.5→DE:CA:FB:AD:00:03→mailserver↵ 10.0.0.6→undefined→undefined↵
Whoah! Wait! It looks like that doesn't work. Do some reading, oh, I need to use the “===” equality operator instead of the “==” equality operator (shades of Lisp here).
for (var i = 0 ; i < table.rows.length ; i++) { var ip = table.rows[i].cells[0].textContent var mac = table.rows[i].cells[1].textContent var note = table.rows[i].cells[2].textContent if (ip === undefined) { ip = "" } if (mac === undefined) { mac = "" } if (note === undefined) { note = "" } textfile = textfile + ip + "\t" + mac + "\t" + note + "\n" }
And I get …
10.0.0.0→00:00:00:00:00:00→Backup network↵ 10.0.0.1→DE:CA:FE:BA:D0:01→mobybackup↵ 10.0.0.2→DE:CA:FE:BA:D0:02→dnsserver↵ 10.0.0.3→undefined→undefined↵ 10.0.0.4→undefined→undefined↵ 10.0.0.5→DE:CA:FE:BA:D0:03→mailserver↵ 10.0.0.6→undefined→undefined↵
That's not working? What's going on here?
Then a lightbulb goes on. The variable note
isn't
undefined
, it's "undefined"
, as in, the
string consisting of the letters u-n-d-e-f-i-n-e-d. I changed the
code used to construct the table:
var lines = req.responseText.split("\n") for (var i = 0 ; i < lines.length ; i++) { var row = table.insertRow(-1) var fields = lines[i].split("\t") var cell; cell = row.insertCell(0) cell.textContent = (fields[0] !== undefined) ? fields[0] : "" cell = row.insertCell(1) cell.textContent = (fields[1] !== undefined) ? fields[1] : "" cell = row.insertCell(2) cell.textContent = (fields[2] !== undefined) ? fields[2] : "" }
And I finally got what I was expecting:
10.0.0.0→00:00:00:00:00:00→Backup network↵ 10.0.0.1→DE:CA:FE:BA:D0:01→mobybackup↵ 10.0.0.2→DE:CA:FE:BA:D0:02→dnsserver↵ 10.0.0.3→→↵ 10.0.0.4→→↵ 10.0.0.5→DE:CA:FE:BA:D0:03→mailserver↵ 10.0.0.6→→↵
Now, I had originally written this as an indictment against dynamically
typed languages, but really, it's not, now that I think about it. C can
also have undefined variables, but they're usually pointers, and they
usually contain NULL
(less rarely, some other illegal address).
I think my problem here was a mental mismatch with what was actually going
on, which I'm finding is all too easy in a dynamically typed language like
JavaScript. Was the variable undefined because it was actually
undefined, or was it "undefined"
?
Would using a statically typed language helped me here? Possibly. It
might have forced the issue of “undefined” at compile time. Or possibly
not (on my system, doing something like printf("%s\n",NULL)
results in “(null)” being printed, which isn't catching the problem at
compile time). But the hiding the issue (or in this case, not crashing)
makes it harder to debug such things.