I came across this question when trying the intanceof()
operator. This code returns true when working with String Objects:
var aStringObj = new String('String');
console.log(typeof aStringObj); // object
console.log(aStringObj instanceof String); // true
... and returns false with Literal Strings, like so:
var aLiteralString = "Test literal";
console.log(typeof aLiteralString); // string
console.log(aLiteralString instanceof String); // false
According to MDN, this operator tests presence of constructor.prototype in object's prototype chain, and for both; the aStringObj and aLiteralString, the .constructor.prototype are the same:
var aStringObj = new String('String');
console.log(aStringObj.constructor.prototype);// String {length: 0, [[PrimitiveValue]]: ""}
var aLiteralString = "Test literal";
console.log(aLiteralString.constructor.prototype); // String {length: 0, [[PrimitiveValue]]: ""}
Does this mean that primitive values are not instances of any prototype? And literal strings don't have a known constructor?
The more I study JS the more dumb questions arise to my head. Thanks in advance.