In the answers here, I didn't read anything about what equal means. Some will say that === means equal and of the same type, but that's not really true. It actually means that both operands reference the same object, or in case of value types, have the same value.

So, let's take the following code:

var a = [1,2,3]; var b = [1,2,3]; var c = a; var ab_eq = (a === b); // false (even though a and b are the same type) var ac_eq = (a === c); // true

The same here:

var a = { x: 1, y: 2 }; var b = { x: 1, y: 2 }; var c = a; var ab_eq = (a === b); // false (even though a and b are the same type) var ac_eq = (a === c); // true

Or even:

var a = { }; var b = { }; var c = a; var ab_eq = (a === b); // false (even though a and b are the same type) var ac_eq = (a === c); // true

This behavior is not always obvious. There's more to the story than being equal and being of the same type.

The rule is:

For value types (numbers):

a === b returns true if a and b have the same value and are of the same type

For reference types:

a === b returns true if a and b reference the exact same object

For strings:

a === b returns true if a and b are both strings and contain the exact same characters

Strings: the special case...

Strings are not value types, but in Javascript they behave like value types, so they will be "equal" when the characters in the string are the same and when they are of the same length (as explained in the third rule)

Now it becomes interesting:

var a = "12" + "3"; var b = "123"; alert(a === b); // returns true, because strings behave like value types

But how about this?:

var a = new String("123"); var b = "123"; alert(a === b); // returns false !! (but they are equal and of the same type)