How can I view array structure in JavaScript with alert()?
Solution 1:
A very basic approach is alert(arrayObj.join('\n'))
, which will display each array element in a row.
Solution 2:
EDIT: Firefox and Google Chrome now have a built-in JSON
object, so you can just say alert(JSON.stringify(myArray))
without needing to use a jQuery plugin. This is not part of the Javascript language spec, so you shouldn't rely on the JSON
object being present in all browsers, but for debugging purposes it's incredibly useful.
I tend to use the jQuery-json plugin as follows:
alert( $.toJSON(myArray) );
This prints the array in a format like
[5, 6, 7, 11]
However, for debugging your Javascript code, I highly recommend Firebug It actually comes with a Javascript console, so you can type out Javascript code for any page and see the results. Things like arrays are already printed in the human-readable form used above.
Firebug also has a debugger, as well as screens for helping you view and debug your HTML and CSS.