DEV Community

loading...

Discussion on: How to Reverse a String

Collapse
rabbitzzc profile image
rabbit

str.split('').reverse()

Collapse
wheatup profile image
Hao • Edited

BTW, this doesn't work for surrogate pairs:

'Hello World ๐ŸŒŽ'.split('').reverse().join('')   // ๏ฟฝ๏ฟฝ dlroW olleH

It's better to use spread syntax:

[...'Hello World ๐ŸŒŽ'].reverse().join('')   // ๐ŸŒŽ dlroW olleH
Collapse
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Does it always work correctly? I mean

[...'๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ']

Normally, I would rather trust this library -- npmjs.com/package/runes2

Collapse
rabbitzzc profile image