Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I view the structure of an array in javascript using alert()?

share|improve this question
9  
This doesn't answer your question, but you should install FireBug and use console.log -- it's great for introspection of JavaScript objects. – Deniz Dogan Jun 9 '10 at 14:15
What do you mean with array structure? You can loop through the array and alert each value. – Tim Schmelter Jun 9 '10 at 14:16
@ Deniz Dogan by console.log do you mean shift+R and option "console" ? – pppttt Jun 9 '10 at 14:24
@Deniz - console.debug would actually work better. – Matt Ball Jun 9 '10 at 14:25
@Tim by structure i mean the index and the value, the "raw" view of array like in PHP printf – pppttt Jun 9 '10 at 14:26
show 4 more commentsadd comment (requires an account with 50 reputation)

9 Answers

A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)

pass your js array to the function below and it will do the same as php print_r() function

 alert(print_r(your array));  //call it like this

function print_r(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects 
    for(var item in arr) {
        var value = arr[item];

        if(typeof(value) == 'object') { //If it is an array,
            dumped_text += level_padding + "'" + item + "' ...\n";
            dumped_text += print_r(value,level+1);
        } else {
            dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    }
} else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
share|improve this answer
This works great! Thx – Dss Nov 21 '12 at 17:59
glad it works for you :) – Maryam Nov 22 '12 at 13:35
this function make firefox crashed with parsing a UI sortable array. – RezaSh Jan 2 at 8:13
add comment (requires an account with 50 reputation)

You can use alert(arrayObj.toSource());

share|improve this answer
1  
Works under Firefox, but not under Safari or MSIE, see stackoverflow.com/questions/1101584/… – Julien Kronegg Dec 20 '12 at 14:24
add comment (requires an account with 50 reputation)

If this is for debugging purposes, I would advise you use a JavaScript debugger such as Firebug. It will let you view the entire contents of arrays and much more, including modifying array entries and stepping through code.

share|improve this answer
add comment (requires an account with 50 reputation)

I'd recommend using toString().

Ex. alert(array.toString()), or console.log(array.toString())

share|improve this answer
add comment (requires an account with 50 reputation)

You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts.

share|improve this answer
add comment (requires an account with 50 reputation)

Better use Firebug (chrome console etc) and use console.dir()

share|improve this answer
add comment (requires an account with 50 reputation)
alert($("#form_id").serialize());
share|improve this answer
The question is tagged javascript which says "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected". Even if that wasn't the case… the question is asking about an array, not an HTML form. – Quentin Jul 18 at 21:44
add comment (requires an account with 50 reputation)

protected by Quentin Jul 18 at 21:43

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.