Hello. I’ve read a couple of Stack Overflow discussions on this, but still am not clear on when it is best to use concatenation vs. the spread operator. For example, can you explain why this code works?..
function diffArr(arr1, arr2){
return arr1.concat(arr2).filter(c => !arr1.includes(c) || !arr2.includes(c));
}
But why this code does not work,?
function diffArr(arr1, arr2){
return [...arguments].filter(c => !arr1.includes(c) || !arr2.includes(c));
}
I wrote the script below to compare the outputs of concat vs. spread …, and they look identical to me.
function testFoo(arr1,arr2){
var fooCat = arr1.concat(arr2);
var fooArg = [...arguments];
var fooGoo = [...arr1,arr2];
return fooCat + " "+fooArg+" "+fooGoo;
}
undefined
testFoo(['a','b','c'],["@","#","!"])
“a,b,c,@,#,! a,b,c,@,#,! a,b,c,@,#,!”```