3 ms·
To be honest, I spent half an hour today debugging some JavaScript I'd written recently that looked something like this: var array = ["a", "b"]; var ot
by Kerrick 13y ago
To be honest, I spent half an hour today debugging some JavaScript I'd written recently that looked something like this:
var array = ["a", "b"];
var otherArray = ["c", "d"];
otherArray.forEach(array.push, array);
console.log(array);
I expected array to become ["a", "b", "c", "d"], but it actually becomes ["a", "b", "c", 0, <Array>, "d", 1, <Array>]. This is because Array#push accepts 1 through n arguments, not just 1 argument.
(I couldn't use Array#concat for reasons that don't show up in this simplified example.)
- TheZenPsycho 13y agopush was designed and made about a decade before "forEach". Your code sample is not surprising or obscure to someone who has skimmed documentation on "push" and "forEach"