Saturday, 31 August 2013

Confused about the behaviour of concat() and the object it returns

Confused about the behaviour of concat() and the object it returns

Consider this example:
var a, b, c, d;
a = new Array(1,2,3);
b = "dog";
c = new Array(42, "cat");
d = a.concat(b, c);
document.write(d); // outputs 1, 2, 3, dog, 42, cat - makes sense
What I don't get is:
alert(b[0]); // d
alert(b[1]); // o
.. etc.
I understand concat() returns a new array object, but why does it slice
the string "dog" into individual array elements? I would've expected b[0]
to return "dog" and b[1] to return undefined. Sorry if this is a stupid
question :/

No comments:

Post a Comment