Skip to content

Instantly share code, notes, and snippets.

@LarryAnomie
Created December 4, 2013 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarryAnomie/7787353 to your computer and use it in GitHub Desktop.
Save LarryAnomie/7787353 to your computer and use it in GitHub Desktop.
print_r equivalent in js
//php print_r equalivalent
//http://scriptnode.com/article/javascript-print_r-or-var_dump-equivalent/
function print_r(x, max, sep, l) {
l = l || 0;
max = max || 10;
sep = sep || ' ';
if (l > max) {
return "[WARNING: Too much recursion]\n";
}
var
i,
r = '',
t = typeof x,
tab = '';
if (x === null) {
r += "(null)\n";
} else if (t == 'object') {
l++;
for (i = 0; i < l; i++) {
tab += sep;
}
if (x && x.length) {
t = 'array';
}
r += '(' + t + ") :\n";
for (i in x) {
try {
r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
} catch(e) {
return "[ERROR: " + e + "]\n";
}
}
} else {
if (t == 'string') {
if (x == '') {
x = '(empty)';
}
}
r += '(' + t + ') ' + x + "\n";
}
return r;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment