skip to Main Content
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


  1. The Object() constructor sees the string you pass, and returns the equivalent of new 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 as let test = {};.

    And it’s not an array, it’s a String instance.

    Login or Signup to reply.
  2. 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:

    let test = new Object('test')
    
    console.log('Instance of:', test.__proto__.constructor.name);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search