4.6 CONVERSIONS

There are in-built ways to convert an object or a primitive into other primitive types. They are done through the methods toString() and valueOf(). In fact, all objects inherit the method toString from Object.prototype. By default, it returns “[object Object]”. This is the reason you will see this output when you print the object. However, you can override the default behavior and it is easy as well.

Similarly valueOf() is another built-in function, with the same default value. Together, these functions are meant to return the most meaningful value belonging to String or Number or Boolean (Figure 4.8). For instance, the outputs (of either method) returned for an Array variable is a comma separated list of value.

 

 

Fig. 4.8 ...

Get Web Technology: Theory and Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.