What is the difference between the below two operations that accounts for the one with join resulting in "÷C " where the other with reduce results in "÷C"?
1
// returns "÷C "
["f7","43"].map(x=>'0x'+ x).map(String.fromCharCode).join('');
2
// returns "÷C"
["f7","43"].map(x=>'0x'+x).reduce((a, c) => {
a += String.fromCharCode(c);
return a
}, '');
String.fromCharCodeaccepts multiple arguments. Each argument will be interpreted as a code unit. In the first code, since.mapalso provides arguments for the index and the array being iterated over:is equivalent to
Which has unexpected results.
Explicitly pass only the
strinstead, and it'll result in the same as in the second snippet:(still, calling
fromCharCodewith something that isn't a number is weird and confusing, better to do it explicitly as Barmar mentions)