Skip to content
Advertisement

javascript – console.log prints an array with its brackets ” [ ]” and not just the content

I have this code

var family = ["Jullia", "James", "Eva"];
console.log(family);

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..)

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement