Home > Software design >  javascript - console.log prints an array with its brackets " [ ]" and not just the content
javascript - console.log prints an array with its brackets " [ ]" and not just the content

Time:12-06

I have this code

var family = ["Jullia", "James", "Eva"];
console.log(family);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

And as you can see, when you run the code, it prints the brackets as well. Why?

(I am new to javascript so i know that this might seem like a stupid question to ask..)

CodePudding user response:

This is simply how your console has chosen to represent arrays—by writing each element enclosed in brackets. It helps the programmer know that what has been printed is an array of elements. I would not rely on this "stringified" version, though, if you're trying to print names in a specific way.

To do that, I would suggest learning about Array.join() (reference), where the examples do exactly as you might want.

  • Related