var_dump for JavaScript – JavaScript equivalent of PHP var_dump

Here’s a handy little JavaScript function that lets you dump the contents of an array, just like you would using var_dump in PHP.

function dump(obj) {
 var out = '';
 for (var i in obj) {
 out += i + ": " + obj[i] + "\n";
 }
alert(out);
}

To use, call the function and pass it the array you want to dump. For example:

dump(field.choices[i]);

The array will appear on screen in an alert window.
You can also change the alert to console.log to make the output display in the browsers debug console. for example:

console.log(out);

var_dumpForJavaScript

Reference: http://stackoverflow.com/questions/323517/is-there-an-equivalent-for-var-dump-php-in-javascript

Was this article helpful?

Related Articles