JS performs
type coercion
or
type conversion
, which means that when 2 different data types are used together in an expression, we get an "equivalent" type.
Example
We know that true
and false
are booleans, not numbers.
But we can still use them as numbers, because due to type coercion, true == 1
and false == 0
.
Not that I am using ==
and not ===
.
Similarly, we can do:
var a = 123;
var b = a + 1; // 124
var c = a + true; // 124
var d = a + '1'; // 1231
In your case, you are coercing an aray and a string. And the JS engine coerces the array to string type:
var a = [1,2,3]
console.log(a) // [1, 2, 3]
console.log(a + '') // "1,2,3"
console.log('{' + a + '}') // "{1,2,3}"