Javascript string test

Literals vs Objects Example with an isoformat date.

by Vincent Audebert

JavaScript

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