From the MDN
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
So, the string literal v1
will be coerced to an object
for the method to work. When the string literal is coerced to an object
, the String() constructor is used, and you will get an object with numeric-properties, something array-like
(object with a length property and indexed elements), but not an array, i.e, actually the same as if you do new String(v1)
:
let v1 = "myString";
let obj = new String(v1);
console.log(obj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
You don't get an Array:
Just for clarification, the coerced object
you get from the string
literal is an array-like
object but not an array
, as you can see on next test that uses Array.isArray() to do the check:
let v1 = "myString";
let strObj = new String(v1);
console.log(Array.isArray(strObj));
let arr = v1.split("");
console.log(Array.isArray(arr));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
For know more about array-like
objects and differences to arrays, maybe you could give a read to next link:
JavaScript - Difference between Array and Array-like object