What is the difference between...
if(myVar.constructor == String)
and
if(typeof myVar == \"string\")
The expression myVar.constructor
just returns the value of the member "constructor". It's completely possible and legal for a given object to have a constructor which points to string (or anything else). For example
function Apple() {}
var x = new Apple();
x.constructor = String
if (x.constructor == String) {
// True
}
Using the typeof
operator though provides the information you're looking for
if (typeof myVar == 'string') {
// It's a string
}