Converting values to strings in JavaScript
Briefly

JavaScript string conversion methods present complexities, particularly with tricky values like null and undefined. The article discusses five approaches, with {}.toString.call(v) being the lone reliable method across all scenarios, though String(v) offers a simpler option for less critical use cases. Understanding the limitations of string representations in objects and arrays is necessary, as default outputs are not useful. Additionally, this ability to customize object stringification through implementing toString() enhances the utility of converted outputs.
Converting values to strings in JavaScript has complexities, and only {}.toString.call(v) handles all tricky values, while String(v) is a simpler option.
Utilizing the syntax {}.toString.call(v) lets us avoid failures from undefined or null receivers and calls .toString() on the value passed.
String representation of plain objects defaults to '[object Object]', which is not insightful. Meanwhile, arrays provide better representations, though they still lack detailed data.
Customizations to object stringification can be achieved by implementing the toString() method, enabling more meaningful outputs beyond the default '[object Object]' format.
Read at 2ality
[
|
]