Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

13. Working with Primitive String, Numbe... > Primitive/Literal Values Are Convert...

Primitive/Literal Values Are Converted to Objects When Properties Are Accessed

Do not be mystified by the fact that string, number, and boolean literals can be treated like an object with properties [e.g., true.toString()]. When these primitive values are treated like an object by attempting to access properties, JavaScript will create a wrapper object from the primitive’s associated constructor, so that the properties and methods of the wrapper object can be accessed. Once the properties have been accessed, the wrapper object is discarded. This conversion allows us to write code that would make it appear as if a primitive value was, in fact, an object. Truth be told, when it is treated like an object in code, JavaScript will convert it to an object so property access will work, and then back to a primitive value once a value is returned. The key thing to grok here is what is occurring, and that JavaScript is doing t....


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint