Javascript Object to String

Javascript is a little hard to debug, Firebug helps a lot, but sometime you just to need to convert a javascript object into a string.
In order to do that (in firefox) you could use the toSource() method.
Here is how you do it:


// create an object
var anyObject = {'someAtt':'someValue', 'otherAtt' : 'otherValue'};
// get the object to source
var anyObjectToString = anyObject.toSource() ;
// now write the string
document.writeln(anyObjectToString );

Here is the printout of this code in firefox:

In Internet Explorer and Chrome this will generate a Javascript error – but this is not for production so using it in Firefox would do fine.

Leave a Reply