It is actually a very reasonable behaviour for how js works. JS when it encounters an object but is typically expecting a string will call the toString() method which is present on all objects. The base case is to return “[object Object]” unless overridden. This can be useful in some albeit niche circumstances, but more importantly it isn’t inconsistent.
The reason your example with BigInt doesn’t work is because a vanilla object doesn’t implement the valueOf method. You will find for example that you can pass a Date to BigInt and it will happily construct due to having valueOf implemented
View all comments
Show parent comment
1
u/SoInsightful Mar 21 '24
Absolutely not.
BigInt({})throws "Uncaught SyntaxError: Cannot convert [object Object] to a BigInt".The only actual explanation for the coercion is "it seemed like a reasonable idea at the time".