Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toString() method behaviour in Objects

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.

like image 543
Thelostcause Avatar asked Jan 17 '26 08:01

Thelostcause


1 Answers

In the first case, user.toString is called implicitly because alert is expecting a string and user is not a string. When user.toString() is called and the return value is not a string an error is thrown.

In the second case, user.toString is called explicitly by you, it doesn't matter if its name is toString or not because it returns the inner object just like any other method would (no primitve value is expected here). The inner object is then passed to alert, and its toString gets implicitly called which returns a string because it isn't overridden. The second case can be broken into two parts for clarity:

let obj = user.toString();    // first user.toString gets called, it will return the inner object
alert(obj);                   // obj.toString will implicitly gets called and since that will return a string everthing will be fine

If the inner object has a toString that returns an object, then the second case won't work either:

let user = {
  name: "John",
  money: 1000,

  toString() {
    return {
      name: 'Bong',
      toString() {                  // toString of the inner object is overridden
        return {};                  // ... and it returns an object
      }
    };
  }
};

alert(user.toString());            // now even the second case won't work because when toString of the inner object is called, a string is expected, but instead an object is returned which is a no no
like image 200
ibrahim mahrir Avatar answered Jan 20 '26 22:01

ibrahim mahrir