let test=new Object('test')
console.log(test) // String{'test'} which is array.
// if I log like below
console.log(test[0]) //'t'
console.log(test[1]) //'e'
console.log(test[2]) //'s'
console.log(test[3]) //'t'
I looked for property of Object but I couldn’t find anything to explain this.. help!@
2
Answers
The
Object()
constructor sees the string you pass, and returns the equivalent ofnew String('test')
. It does that for all the primitive types. If you pass an object, you get back that object. If you pass nothing, you get back a new empty Object instance, essentially the same aslet test = {};
.And it’s not an array, it’s a String instance.
As mentioned in the other answer, the primitive types are converted to corresponding primitive object instances. You can always get which instance class is an object of: