Lately, I’ve been doing some coding challenge and I saw this problem. This is fairly easy challenge and the common solution to this is something like this
function reverse(s){
return s.split("").reverse().join("");
}or with ES6 using reduce function
function reverse(s) {
return s.split('').reduce((reversedString, nextCharacter) =>
nextCharacter + reversedString
, '');
}Easy peasy.
After answering this problem, I started searching on Stackoverflow because I’m on curious how others solved this problem.
Then I saw this, https://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript#answer-16776621
It makes my solution and others wrong because of this unicode problem.
So basically if I used my function with this string
reverse('foo 𝌆 bar mañana mañana');The output will be

With a slight change on my ES6 solution, we can fix this problem by using spread operator because this operator is unicode-aware.
My new solution is now like this
function reverse(s) {
return [...s].reduce((reversedString, nextCharacter) =>
nextCharacter + reversedString
, '');
}More info: