13 Aug 2009

Fun With Wacky JavaScript Type Comparison

7 Comments Uncategorized

I recently had a conversation with Scoot Koon (LazyCoder) over Twitter about the wacky JavaScript type comparisons that are allowed.  I was interested to see what weird oddities would come out if I compared the whole type system against it self.  So I sat down and wrote a simple JavaScript routine to do just that, and the below reference table is the output of that routine.

null undefined true false -1 0 1 NaN Infinity “” ” “ “null” “undefined” “true” “false” “-1″ “0″ “1″ “NaN” “Infinity”
null null null
undefined undefined undefined
true true true true
false false false false false false
-1 -1 -1
0 0 0 0 0 0
1 1 1 1
NaN
Infinity Infinity Infinity
“” “” “” “”
” “ ” “ ” “ ” “
“null” “null”
“undefined” “undefined”
“true” “true”
“false” “false”
“-1″ “-1″ “-1″
“0″ “0″ “0″ “0″
“1″ “1″ “1″ “1″
“NaN” “NaN”
“Infinity” “Infinity” “Infinity”

So some of the oddities that emerged to me are:

  • The word “Infinity” is equal to the type Infinity, however “true” or “false” don’t equal true or false respectively.
  • " " == 0 == false and also "" == 0 == false, however " " != ""
  • Update Just noticed that NaN != NaN but Infinity == Infinity

If you would like to try this your self, or want to add to it, here is the code that I used.

var values = [null, undefined, true, false, -1, 0, 1, NaN, Infinity, "", " ", "null", "undefined", "true", "false", "-1", "0", "1", "NaN", "Infinity"];

document.write("<table><thead><tr><th></th>")
for (var x = 0; x < values.length; x++) {
	document.write("<th>" + (x > 8 ? """ : "") + values[x] + (x > 8 ? """ : "") + "</th>");
}
document.write("</tr></thead><tbody>");
for (var i = 0; i < values.length; i++) {
	document.write("<tr>");
	document.write("<th>" + (i > 8 ? """ : "") + values[i] + (i > 8 ? """ : "") + "</th>");

	for (var j = 0; j < values.length; j++) {
		var output = values[i] == values[j];

		document.write("<td style="text-align:center;" + (i == j ? "background-color:black;" : (output ? "background-color:green;color:#00AF33;" : "color:#e0e0e0;")) + "">");
		document.write(output ? (i > 8 ? """ : "") + values[i] + (i > 8 ? """ : "") : "--");
		document.write("</td>");
	}

	document.write("</tr>");
}
document.write("</tbody></table>");

I think Scott really hit the nail on the head when he said this about JavaScript coercion.

LazyCoder (Scott Koon) on JavaScript Coercion

16 Apr 2009

Recession Proof Your Programming Skills

3 Comments Uncategorized

In this economy you have to do everything to keep your skills fresh and current so that employers find you a desirable hire.  I really though the tips provided in 8 Ways to Recession-Proof Your Programming Career where spot on when this article came out last year.  And now that the TechRepublic has released 10 kills developers will need in the next 5 years.  I have decided to give you some of my favorite Wrox books that align very well to this TechRepublic article.

You can now find this list on Amazon.com under the same title, Recession Proof Your Programming Skills

Learn C#

Learn ASP.NET

Learn ASP.NET MVC

Final Cover Photo

didn’t think I would leave my book out, did you? ;)

Learn Java

Learn PHP

Learn RIA & Web 2.0

I beleive all these books are a nessisty in helping you improve your career.  You don’t have to understand or know all of this technology, but you should at least have one of these books on your shelf.

08 Dec 2008

Creating a Progressive Queue in IE6 JavaScript

No Comments Uncategorized

Today IE6 really kicked my butt, I had the following code working in FireFox, IE7, etc, all except IE6:

ShowLoadingPanel();
// execute some code here
HideLoadingPanel();

The above code is really simple, it shows a full screen loading panel when I am executing some code in between the Show and Hide functions for the loading panel. It worked in every browser that I tried except for IE6. This seemed to occur because the execution of the JavaScript was happening in the same thread as the UI rendering of the screen, which would block all screen updates to the browser until the JavaScript was executed. By the time all the JavaScript is done rendering the UI is in it’s final visual state, so it looks like the loading graphic was never run. Note I don’t know for sure that this is what is really happening, but it definitely seems like the most likely possibility. But if this is true it really explains the lack of support for JavaScript Animation, which heavily relies on a progressive incremental rendering of the UI.

On my way back home from work after dealing with this for a while, a light bulb clicked on in my head and it occurred to me. That I might be able to use the setTimeout function to force the execution in to a different thread, which would free up the current thread to render the loading screen.

ShowLoadingPanel();
setTimeout(function() {
    // execute some code here
    HideLoadingPanel();
}, 500);

Basically what the above is doing is creating a slight delay in the execution of the code. Which releases the current thread to render the loading screen to the browser. After that is done the rest of the code, in the setTimeout, is executed in the background and locking up a different thread. The important thing to remember is to keep the method that hides the loading panel inside the setTimeout so that the hide panel method is executed in a different process and after the code that you want to execute.

So that is how you create a progressive queue in IE6 for JavaScript. Until next time I hope you found this useful.