What’s the Difference Between Equal and Identical?

Is it just me, or does saying equal seem to be the same as saying identical? Actually, it’s not, and I’ve struggled with understanding this for a while, until now! I figured out that they were different, through Javascript. The == and === comparison operators are not the same.

Here are the differences in definition in the Merriam-Webster’s Online Dictionary:

equal
      1. of the same measure, quantity, amount, or number as another
      2. identical in mathematical value or logical denotation : equivalent
    1. like in quality, nature, or status
    2. like for each member of a group, class, or society
  1. regarding or affecting all objects in the same way : impartial
  2. free from extremes: as
    1. tranquil in mind or mood
    2. not showing variation in appearance, structure, or proportion
    1. capable of meeting the requirements of a situation or a task
    2. suitable
identical
  1. being the same : selfsame
  2. having such close resemblance as to be essentially the same
    1. having the same cause or origin
    2. monozygotic

I guess I thought these definitions should be the same, in Javascript, because equal is identical in mathematical value or logical denotation. It turns out that in Javascript, equal is identical in value, but not necessarily in type. So, they are different.

Here’s the short, simple, Javascript explanation:

if ("1000" == 1000) {
     alert("They are equal");
} else {
     alert("They are not equal");
}

This code will show an alert box that says “They are equal”. But, let’s try this:

if ("1000" === 1000) {
     alert("They are identical");
} else {
     alert("They are not identical");
}

This code will show an alert that says “They are not identical”, because they are different types. One is a string and the other is a number.

if ("1000" === new String(1000)) {
     alert("They are identical");
} else {
     alert("They are not identical");
}

This will show an alert box saying “They are identical”. They are now both strings.

Get it? Got it? Good!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • FriendFeed
  • Identi.ca
  • Netvibes
  • Reddit
  • StumbleUpon
  • ThisNext
  • Tumblr
  • Twitter

2 Comments

  1. Tee Hamburg wrote:

    Hey, interesting post! bookmarked :)

    Thursday, March 11, 2010 at 12:17 am | Permalink
  2. strimble wrote:

    Hey, thanks Tee. I’m glad that you found it interesting. :)

    Tuesday, May 4, 2010 at 3:25 pm | Permalink

Post a Comment