Isn't the string an instance of String?

clipboard.png

as you can see from the figure, the _ _ proto__ attribute of a points to the prototype, of String, which means that an is an instance of String.
but why is instanceof false?

May.22,2021

it turns out that instanceof cannot be used to determine the basic library type.


it is best to use literals for basic data types

var a = '122'
console.log(typeof a) // string
console.log(b instanceof String) // false

var b = new String('122');
console.log(typeof a) // object
console.log(b instanceof String) // true

//  
console.log(Object.prototype.toString.call(a)) // [object String]
console.log(Object.prototype.toString.call(b)) // [object String]

Packaging types

Menu