Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading