When accessing a property on a string in JavaScript, the runtime creates a new String object and copies the string primitive value into it. The property or method is then called on this wrapper object before being garbage collected once the statement is complete. This gives the illusion that certain primitives have properties when they actually do not.
If so, what purpose does distinguishing between primitive data types and objects serve?
The distinction lies in primitives being lightweight entities with low memory usage, while Objects offer more flexibility but require a larger memory footprint. Additionally, inheritance is not possible with primitives like it is with Objects.
If your operations involve limited use of Object properties, using a primitive is ideal. However, if you anticipate extensive Object property usage, it's more efficient to create an Object from the start with a constructor to avoid constant creation and destruction of wrapper objects.