Creative Juices Bo. Co.

Satisfy Your Thirst For Something Refreshing!

Use Idioms To Make Your Intentions More Concise

To "===" or not to "=="

About 6 months ago, I came across an online tool called JSLint. It's a JavaScript program that looks for problems in JavaScript code. I began using it to check most of my scripts to see if I had any errors in them (I know, I know... you thought I was perfect). Well as it turns out, I had a lot of little mistakes, like using a function or object before it was defined or little formating issues. But one error I kept receiving made no sense to me and I honestly didn't understand why I was getting it.

Expected '===' and instead saw '=='.

Expected '!==' and instead saw '!='.

Granted, it wasn't like I didn't understand how to fix the issue, but JSLint wasn't very forthcoming on why I needed to make these changes. So, without fully understanding what wasgoing on, I began using "===" and "!==" whenever I needed to make a logical comparision. And I've been doing this for the past 6 months, without a clue as to "why".

Say hello to my little friend, "why"

I'm not going to lie, every time I typed out "===" I said to myself, "Why am I doing this?!?". So I started looking around. Google and Yahoo were not a big help, because they like to search for text, not special characters. But I eventually came across a nice little paragraph that explained the whole thing.

First, let's take a look at an example script:

var x = 1;

if (x == true) {

   alert ("I'm a fuzzy match!");

} else {

   alert ("I'm strict, go away!");

}

If you run this script you will see an alert box pop up that says "I'm a fuzzy match!". Awww... isn't that nice? But let's take a look at that function. We first set a variable named X to a value of 1. And then we test to see if X equals TRUE. Because we all know, that 1 equals TRUE, right? Well, actually that's an assumption. Or more precisely, that's called "type coercion". And for the most part we can get away with this kind of "truey" and "falsey" behavior.

Let's try again, by changing the logic "==" to "===":

var x = 1;

if (x === true) {

   alert ("I'm a fuzzy match!");

} else {

   alert ("I'm strict, go away!");

}

How rude. Now we get the "I'm strict, go away!" message. The reason for this, is in reality X equals 1, not TRUE. This time we tested to see if X is the BOOLEAN value TRUE. It's not, we performed a strict comparision. So basically 1 == TRUE equals true, but 1 === TRUE equals false.

Conclusion

If your asking "What does this all mean and when and where should I use this?". I'm not sure I have that answer yet. I really don't understand a situation where I need to perform a strict test like this. Perhaps there will be a time when I have a variable that could have possible values of 1, true, 0 and false, or maybe a STRING value of "foo" and not an OBJECT named "foo", but I really can't recall, in my years of programming, a time when this mattered. Perhaps, this might help performance, I'm not sure. I don't think my skills are at the level where this makes a whole lot of difference. I guess will have to wait and see whenever the Web 2.0 era is in full swing.

So, even though there's still some question as to the "When", at least we now have a better understanding as to the "Why." If you have any insight into this, I would love to hear about it.

P.S. This is the website where I found the answer: http://javascript.crockford.com/style2.html