JS var_dump() / print_r() function

by Jared Williams

JavaScript

/**
 * Concatenates the values of a variable into an easily readable string
 * @param {Object} obj The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} level The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r( obj, max, sep, level ) {
    level = level || 0;
    max = max || 10;
    sep = sep || ' ';

    if( level > max ) return "[WARNING: Too much recursion]\n";
    
    var i, result = '', tab = '', t = typeof obj;

    if( obj === null ) {
        result += "(null)\n";
    } else if( t == 'object' ) {
        level++;

        for( i = 0; i < level; i++ ) { tab += sep; }
        if( obj && obj.length ) { t = 'array'; }
        
        result += '(' + t + ") :\n";
        for( i in obj ) {
            try {
                result += tab + '[' + i + '] : ' + print_r( obj[i], max, sep, (level + 1) );
            } catch( error ) {
                return "[ERROR: " + error + "]\n";
            }
        }
    } else {
        if( t == 'string' ) {
            if( obj == '' )    obj = '(empty)';
        }
        result += '(' + t + ') ' + obj + "\n";
    }
    return result;
};
var var_dump = print_r;



// EXAMPLE 1
var myVar1 = {'true' : true, 'false' : false, num : 3};
alert(print_r(myVar1));

// EXAMPLE 2
var myVar2 = [{'legend': 'zelda'}, {'foo': { 'bar': null }}, 'a', 'b', [1, 2, { 'key': 'value'}, 'bill', 'ted']];
alert( print_r( myVar2, 10, "\t" ));

// EXAMPLE 3 (this one outputs a LOT, even with a max of 5)
var myVar3 = document.body;
alert( print_r( myVar3, 5 ));