• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Differences in converting javascript errors into Strings

Resolved: Differences in converting javascript errors into Strings

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

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 valueOf() if 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 String(value) and '' + value act the same (coercion from using template string would also act the same as string concatenation), though I would need to check the JavaScript spec to be 100% sure on this. In particular, it sounds like both of these will call 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 Symbol.toPrimitive or valueOf() when converting.

If you have better answer, please add a comment about this, thank you!

javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to resolve LNK2001 in c++ projects

26/03/2023

Resolved: How to correctly initialize vector of number in Typescript

26/03/2023

Resolved: mysql calculating to get profit or loss from 2 tables ( Purchases and Sales) in FIFO mode

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.