The term for this behavior is boxing.
In theory, it does exactly that - create a new string object, and use that. However it should be noted, that in reality, engines internally can take a lot of shortcuts, and make many optimizations.
When reading the spec for 6.2.4.8 GetValue, you can see this boxing in the steps (use ToObject(base)
, if it's a primitive), as well as an explicit note towards the permitted optimization:
The object that may be created in step 5.a.ii is not accessible outside of the above abstract operation and the ordinary object [[Get]] internal method. An implementation might choose to avoid the actual creation of the object.
Therefore, to know what really happens, you'd probably have to read the code of the related engine. For normal javascript programmers, knowing the engine internals is imho not necessary, but if you want to anyways, e.g. V8 is open source (written in c++).