Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.map automatically appends commas when concatenated in a string?

I am a bit confused by the behavior of Array.map function here:

var arr = ['one', 'two', 'three'];
var result = '';
result += arr.map(function(elm) {
    return elm;
});

// 'one,two,three'

How does it automatically join the returned results with a , ?

Note: This only happens if I concatenate the returned result in a string.

like image 543
Avijit Gupta Avatar asked Dec 13 '15 10:12

Avijit Gupta


1 Answers

Array.map did nothing to your array.

You basically did this

'' + ['one', 'two', 'three']

Which calls the toString() method of an array, whose default behaviour is to join(',') the array.

like image 55
Prashanth Chandra Avatar answered Nov 14 '22 22:11

Prashanth Chandra