0

I am new to javascript basics and am learning it now.

So I would like to know why the following code has an additional 'Value' column for 'name' variable instead of considering it as a normal variable and assigning each string to an index.

var name=['Jane','Beulah','Jai'];
var age=[21,21,20];
var city=['BLR','COI','CHN'];
console.table([name,age,city]);

Actual Output:

I seem to get the correct output if I change the variable from 'name' to someother different name. Is it because name is a keyword for console.table()? or is it because of some other reason?

Keerthana
  • 55
  • 7
  • `name` in this scope refers to `window.name`, which is a string property. That is why it implicitly adds a `toString()` which results in `"Jane,Beulah,Jai"`. You should rename it or change its scrope. Yet better rename it to avoid confusion. – Lain Jul 20 '20 at 14:05
  • 1
    Does this answer your question? [What is the \`name\` keyword in JavaScript?](https://stackoverflow.com/questions/2663740/what-is-the-name-keyword-in-javascript) Your case is in the answers as well. – Lain Jul 20 '20 at 14:08

1 Answers1

0

As @Lain said in the comment, name gets confused with the global window.name.

Changing to names will solve the issue: (console.table only prints in browser console, not in stackoverflow console)

var names=['Jane','Beulah','Jai'];
var age=[21,21,20];
var city=['BLR','COI','CHN'];
console.table([names,age,city]);
Greedo
  • 3,438
  • 1
  • 13
  • 28