Skip to content Skip to sidebar Skip to footer

What Happens When I `object.assign` To A Primitive Type In Javascript?

I discovered that you can call Object.assign on a string or number type in javascript to get a sort of 'enhanced' primitive type. // closure used to create object for simplicity fu

Solution 1:

In JavaScript, almost all primitive types have an equivalent object type. So while "x" is a string primitive, new String("x") is a String object. Similarly, there are number primitives and Number objects, and boolean primitives and Boolean objects, etc.

The first step of Object.assign is to take the first argument and use the abstract ToObject operation on it, which performs that primitive-to-object step. From that point forward, it uses the object returned by ToObject. (ToObject just returns its argument if it's already an object.)

That object is what assign ultimately returns.

Post a Comment for "What Happens When I `object.assign` To A Primitive Type In Javascript?"