Question:I’m refactoring some code and I want to make sure I’m not missing any edge cases. I don’t know if I understand all the details of template literals.
Are there any reasonable cases where these three log messages will be different?
Answer:If you follow the good practice of always throwing an instance of
Error, then you can reasonably assume they’ll always be the same. Even if you don’t follow this good practice, these will probably behave the same. It takes some odd tricks to make them behave differently.
Here’s one example, where I’ve created a null-prototype object (so it doesn’t have a default toString() method), then I provided a valueOf() definition to this object and threw it. String coercion will fall back to using
toString()is not present, so the first two logs work. The last one will not, because it’s explicitly trying to use the
toString()function that does not exist.
For further reading, I would look at this section of MDN’s toString page. It seems to allude that using
toString(), unless this Symbol.toPrimitive function is defined on the object, in which case that would take higher precedence.
In general, it seems you’re safest if you swap between
String(error)and concatenating a string with your value. Using
.toString()has the most differences, as that won’t take into account the potential presence of
If you have better answer, please add a comment about this, thank you!