In JavaScript, when you want to know what a given element’s node name is, you use its nodeName property. Now as far as I can tell, it always returns the node’s name in uppercase, so an anchor’s node name returns as A and a table’s TABLE. I have yet to see a browser return a node name in lower case or mixed case. So here’s my question, why is it that I keep seeing the following code all over the internet?

if (el.nodeName.toLowerCase() === "table") {

Is it because there actually are edge cases where browsers return node names in something other than uppercase? Or is it because we just want to be sure, and in our paranoia we normalize the returned value just in case? I want to know because though it may only cost milliseconds at best, toLowerCase is still an extra method that needs to be executed whenever a node name is checked, and it could get heavy inside large loops (now I’m being paranoid). It’s also cumbersome and annoying to have to write, especially if it isn’t needed. I’d much rather leave it at:

if (el.nodeName === "TABLE") {

So is there a reason, or are we all just to scared to say “the Emperor has no clothes?”


Read more from the archive.