Overriding “toString()” function of JavaScript Objects

Mayank Gupta
TechnoFunnel
Published in
2 min readOct 4, 2019

--

Understanding and Overriding the “toString()” method of Objects…

The “toString()” method represents the string representation of Object… Every Object has a “toString()” method which is called automatically when the Object has to be represented as some text value or in the case when the string is expected out of it. The simplest example of this is:

In the above example, we are enclosing the Object inside a “console.log” and “alert” function, which expects the output to be “string” value. Once there is an expectation to get the string value of an object, the following output is generated…

The value that is displayed in the alert box for “userDetails” seems to be unexpected. When we are trying to get the string representation of an object, it returns us the value “[object Object]”. Now, let's drill deeper to understand this output.

--

--

Mayank Gupta
TechnoFunnel

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue https://medium.com/technofunnel