If I enter the following code into the JavaScript console, it returns the number 1310, but why?
console.log(131+"".split(',').map(Number));
I'm adding "" to 131 to convert it to a string, then using the split function to convert that string into an array, and the map function to make sure the values in the array are Numbers.
I'm not sure where the 0 is coming from after the 131, can anyone explain this?
"".split(',')produces.splitmethod to convert""into an array. It's131+("".split(',').map(Number));not(131+"").split(',').map(Number);(which would give[131]) or (as I'd guess you actually wanted)(131+"").split('').map(Number)->[1, 3, 1].+for string concatenation/conversion isn't ideal. I'd recommend using a template string for most cases if your browser support allows it.String(num)Number()is doing that.