Search code examples
javascriptjsonglobal-variablesconsole.log

Console.log not outputting json


var _json = [{"codigo": "1", "nome": "Robert Santos", "turma": "EM2A"}, {"codigo": "2", "nome": "Eduardo Alves Silveira", "turma": "EM3A"}, {"codigo": "3", "nome": "Amara Gouveia", "turma": "EMT2B"}, {"codigo": "4", "nome": "Tainá Belchior da Silva", "turma": "EF5B"}];

function console() {
  console.log(JSON.stringify(_json, null, 4));
}

console();

Hi, i'm using this code, but when I try execute console(), i'm getting this error: TypeError: console.log is not a function

What should I do to fix this problem?


Solution

  • You're getting TypeError: console.log is not a function because you're overwriting the console object in the global scope with your own function called console which doesn't have a log function. You can solve this issue by renaming your function to something else, snippet example:

    var _json = [{"codigo": "1","nome": "Robert Santos","turma": "EM2A"}, 
    {"codigo": "2","nome": "Eduardo Alves Silveira","turma": "EM3A"},{"codigo": 
    "3","nome": "Amara Gouveia","turma": "EMT2B"},{"codigo": "4","nome": "Tainá Belchior da Silva","turma": "EF5B"}];
    
    function log() {
        console.log(JSON.stringify(_json, null, 4));
    }
    
    log();