Possible Duplicate:
Why does instanceof return false for some literals?
If I do...
[] instanceof Array;
...it returns true
although I haven't used new Array()
.
But if I do...
"" instanceof String;
...it returns false
because I haven't used new String()
.
Why? I understand that []
is a language construct for creating arrays and ""
is a language construct for creating strings. So I don't understand why one returns true
and the other returns false
.
Moreover, all of the following codes return true
:
[] instanceof Array; /* true */
Array() instanceof Array; /* true */
new Array() instanceof Array; /* true */
But with strings:
"" instanceof String; /* false */
String() instanceof String; /* false */
new String() instanceof String; /* true */
Shouldn't String() instanceof String
return true
too?
Edit:
I have made a new question (spin-off of this one): Easy way to check if a variable is a string?