this works as expected
[97,98].map(function(x){String.fromCharCode(x)})
// [ 'a', 'b' ]
but the output is following line is unexpected
[97,98].map(String.fromCharCode)
// [ 'a\u0000\u0000', 'b\u0001\u0000' ]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
String.fromCharCodecan accept a variable length of arguments, and treats each one as a character code to build a stringarguments.lengthcharacters long.mappasses several arguments to the inner function. The first, obviously, is the value of the current item. The second is the index in the array, which is where the\u0000and\u0001come from (add more character codes and you get\u0002,\u0003…). The third argument is a reference to the array that is being traversed, which is converted to the number0.Source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
EDIT much, much later: An alternative approach: