Max Doung
Max Doung

Reputation: 221

how does the console.log omit spaces in the array?

console.log( '"' +[ 'apple', 'orange', 'banana', 'strawberry' ] + '"' )

Hello everyone, can someone please expalin how does the console.log() omits the spaces in the array ?

The output should look like that : "apple,orange,banana,strawberry"

How does the console.log do that?

Upvotes: 2

Views: 607

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075209

It's not the console, it's Array#toString, which calls Array#join with no arguments, so Array#join defaults the separator to ",". Array#toString is being called because you're using + on a string and an array, which will coerce the array to string before doing the concatenation.

Example using join explicitly:

var str = ['apple', 'orange', 'banana', 'strawberry'].join();
console.log(str);

Upvotes: 4

Related Questions