🌟object -> primitive conversion
JS ⟩ value ⟩ type ⟩ conversion ⟩ object -> primitive
JS spec defines 3 algorithms for object -> primitive conversion:
prefer-string:preferring a string (if conversion is possible)
prefer-number:preferring a number (if conversion is possible)
no-preference:classes choose their own preference.
⬇️ object -> primitive algorithms are involved in:
⛔ object -> primitive algorithms may result in TypeError❗
Object.prototype.toString() - object to string.
Object.prototype.valueOf() - object to (primitive) value, if exists.
Last updated
Was this helpful?