So I was reading a book of Js and came across this beautiful topic of Object to primitive conversion. In the book the author says
For historical reasons, if toString or valueOf returns an object, there’s no error, but such value is ignored (like if the method didn’t exist). That’s because in ancient times there was no good “error” concept in JavaScript.
Now I tried this , but not getting as said in the book:
let user = {
name: "John",
money: 1000,
toString(){
return {
name: 'Bong'
};
}
};
alert(user);
I got the error in the console saying :
Uncaught TypeError: Cannot convert object to primitive value
However, If I do this
alert(user.toString());
Then there is no erro, It gives [Object object]
Please explain this behaviour.