O'Reilly logo

Web Technology: Theory and Practice by M. Srinivasan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required