2

See the following comparisons:

("a" == "a")
true

(/a/ == /a/)
false

Why do I get different results with the following expressions?

EDIT:

Now I am using just == and still getting the same results.

1
  • 3
    The second one is not a string match. String match will be "/a/"=="/a/" for that. Commented Dec 5, 2014 at 3:49

2 Answers 2

6

The literal /a/ is not a string, it's a regular expression object. No two objects are === to each other.

Sign up to request clarification or add additional context in comments.

3 Comments

Two object instances are always != to each other, even if they are of the same type and have all their respective properties equal.
@ReginaFlats just try typeof /a/, you'll get the answer.
Object instances are only equal if it is the same instance. You can do var a = /a/; and then a === a or a == a are true.
4

undefined, null, Numbers, Strings literals and Booleans are value types in javascript.

So, they will be compared with their value unlike RegExp(or any other Object for that matter) which is an object, where the comparison takes place on the grounds of reference. So "a" == "a" will return true because the values are same but /a/ == /a/ will return false because the references are different.

Comments