identity checks with JavaScript

Every now and than, somebody asks why I am sometimes using three equal signs (===) instead of two (==), when comparing values.

Simple answer is, that I am interested in the identity (type and value) instead of only the value.

Some code:


alert(2 == "2");
alert(2 === "2");

The first one returns TRUE, because the value is equal. But not their types (number vs. string), therefore the second return FALSE.

Howdy!

Posted in javascript

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

%d bloggers like this: