Search code examples
javascriptjconsoleconsole.log

can i create a custom console log function in js?


is there a way to make a function that runs console.log for a string of variables. running once for the name of the variable name and once to show its val

function multilog(text){
    var text=text.split(",")
    for(i=0;i<text.length;i++){
        console.log(text[i]+': ')
        console.log(JSON.stringify(text[i]));
    }
}
multilog('number_words,number_paragraphs,relatedwords');

an example of desired output

number_words: 1 number_paragraphs: 2 relatedwords: [example example example]


Solution

  • console.multilog = function(str){
        var toLog = str.split(',');
        for (var i = 0, len = toLog.length; i < len; i++){
        console.log(toLog[i] + ' =>');
        console.log(eval(toLog[i]));
        }
    };