Reputation: 221
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
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