A better object.toString() in Javascript

August 6, 2008 • 1 min read


When working in Javascript, I often come across a simple object that I want to dump to the console. In Firefox, you have Firebug’s great console commands, so dir() will solve your problem. In Safari or Internet Explorer, though, many of the methods that you’d hope to return a string representation of your object simply return “[object myObject]”.

Thankfully, it turns out you can call Prototype’s Object.toJSON(myObject) to get a crisp string representation of your object to dump to the Safari console, a simulated console, or even an alert(). Not the original intention of JSON, but it works!


Liked this? Follow along to see what's next.

© Allen Pike. 👋🏼 You can contact me, or check out Steamclock.