Monday, January 19, 2015

JavaScript toString() and valueOf() methods

toString() and valueOf() for the Object class

Object is the base class for every object in JavaScript. The Object class has two methods toString and valueOf that serve for conversion purpose and every JavaScript object inherits these two methods.
  • the toString() method returns a string representing that object
  • the valueOf() method returns a primitive value representing that object

toString() and valueOf() for JavaScript native specialized classes

Some specialized Object types re-define their specific versions of the toString() and valueOf() methods to return different values.

toString() and valueOf() for browser host objects

JavaScript host environments (like browsers) come with their objects which represent the DOM elements of an HTML page and the BOM elements of a browser. Also these objects override toString() and valueOf() methods, but the implementations details are left to browser vendors, so the result of following code may differ from one browser to another.

No comments :

Post a Comment