0

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?

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
Karlna
  • 67
  • 2
  • 11

3 Answers3

5

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();
pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
1

The global window object already has a function named console, which you are trying to override with your own console function. Just rename your console function to something else.

H. Majury
  • 356
  • 1
  • 2
  • 8
1

You cannot name your function console

Reason - Console is an existing object in JavaScript that provides access to the browser's debugging console. Read MDN web docs to know more about it

Corrected Code

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 logger() {
    console.log(JSON.stringify(_json, null, 4));
}

logger();
nkshio
  • 1,060
  • 1
  • 14
  • 23