-4

I have created a new Array by Object constructor, why its nature is still array

myArr = [1,2,3];
var newArr = Object(myArr);
if (newArr instanceof Array) {
  console.log('This to is an array');
}
console.log(newArr);

enter image description here

RobG
  • 142,382
  • 31
  • 172
  • 209
Atul Kumar
  • 83
  • 1
  • 6
  • 1
    You might start by reading the relevant part of the language specification, [*ECMA-262*](http://ecma-international.org/ecma-262/8.0/#sec-object-constructor). – RobG Mar 06 '18 at 03:49
  • if you want to convert array to object see this question, [how to convert array to object](https://stackoverflow.com/questions/4215737/convert-array-to-object) – Vikum Dheemantha Mar 06 '18 at 03:59
  • When called as a constructor it creates a new ordinary object. When Object is called as a function rather than as a constructor, it performs a type conversion. thanks RobG – Atul Kumar Mar 06 '18 at 04:02
  • @AtulKumar—yes, the key is that an Array is [*Type*](http://ecma-international.org/ecma-262/8.0/#sec-object-type) Object (noting that *typeof* doesn't return the Type, good eh?), so `Object(myArr)` just returns *myArr*. ;-) – RobG Mar 06 '18 at 04:11

1 Answers1

1

According to docs:

The Object constructor creates an object wrapper for the given value. If the value is not null or undefined, it will return an object of a Type that corresponds to the given value. (adapted)

guijob
  • 4,413
  • 3
  • 20
  • 39