Sometimes it’s good to remind the basics.
Today, I was trying to test if a Date
object that I made becoming a string using toISOString()
was a string.
And I immediately thought about if(date instanceof String)
but this was always false
…
And then I found this post: http://stackoverflow.com/a/21605936
It was painful to see how sometimes you forget that Javascript can be crap and you need a basic reminder.
I created a little JSFiddle to show exactly what I wanted to do and to make obvious the difference between literals and objects. But you can also see the code just here:
var date = new Date();
var dateStr = date.toISOString();
console.log(dateStr instanceof String); //false
console.log(typeof dateStr === "string"); //true
var dateStrObj = new String(dateStr);
console.log(dateStrObj instanceof String); //true
console.log(typeof dateStrObj === "string"); //false
//Recommendation
//For more security keep testing the two ways
console.log(dateStr instanceof String || typeof dateStr === "string"); //true
console.log(dateStrObj instanceof String || typeof dateStrObj === "string"); //true
Finally, another link you might find useful on the Literals Vs Objects topic: http://stackoverflow.com/questions/203739/why-does-instanceof-return-false-for-some-literals