DEV Community

Cover image for Reverse a String in JavaScript

Reverse a String in JavaScript

Swarnali Roy on September 06, 2021

Reversing a String is indeed one of the most common and needed operations in JavaScript. During the journey of any Software Developer's career, a v...
Collapse
 
star_trooper profile image
Atharva Shirdhankar

After long time you published blog,
The blog you write is in well formated way✨.
Thanks 👍

Collapse
 
swarnaliroy94 profile image
Swarnali Roy

Thank you so much, keep following and supporting ☺️ ☺️

Collapse
 
star_trooper profile image
Atharva Shirdhankar

Hey can I suggest you one thing!

Thread Thread
 
swarnaliroy94 profile image
Swarnali Roy

Yes definitely you can..

Thread Thread
 
star_trooper profile image
Atharva Shirdhankar

Can you add snippet of code in such format into your blog.
dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
star_trooper profile image
Atharva Shirdhankar

Even I have used in my blog the same snippet
dev.to/star_trooper/views-and-it-s...

And I have not written the blog in well formatted (text)way but yes due the snip format it is looking great.

Thread Thread
 
swarnaliroy94 profile image
Swarnali Roy

I haven't tried this ever. How can I get this format? Is it any app like vs code? Let me know. i will try

Thread Thread
 
star_trooper profile image
Atharva Shirdhankar

You can make your own snippet using "carbon" snippet creator.

carbon.now.sh/

Thread Thread
 
swarnaliroy94 profile image
Swarnali Roy

Oh thanks a lot! I'll keep this on my mind. :D

Collapse
 
rafipiccolo profile image
Raphael Piccolo

❤️

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const reverseStr = str => [...str].reduce((r,s)=>s+r)

reverseStr("Hello")  // olleH
Enter fullscreen mode Exit fullscreen mode
Collapse
 
swarnaliroy94 profile image
Swarnali Roy

Oh yeah, its another method which I didn't mention, I can add it now. Thank you for reminding ☺️

Collapse
 
atnbueno profile image
Antonio Bueno • Edited

The regex can be simpler (a period is already a single character, and case sensitivity does not affect it):

let regex = /./g
Enter fullscreen mode Exit fullscreen mode
Collapse
 
swarnaliroy94 profile image
Swarnali Roy

Thank you for the suggestion. This is a better one

Collapse
 
tracygjg profile image
Tracy Gilmore

Lovely article Swarnali. I have another approach for you to consider:
const reverseString = ([...str]) => str.reverse().join('');

console.log(reverseString('abcdefg')); // 'gfedcba' on console.

Collapse
 
swarnaliroy94 profile image
Swarnali Roy

This is another good approach, I didn't know about this one before. Thank you so much!!

Collapse
 
linrstudio profile image
Linr

thank you! but emoji?

Collapse
 
swarnaliroy94 profile image
Swarnali Roy

Sorry, I didn't understand , do you want to know from where I'm getting the emojis?
webfx.com/tools/emoji-cheat-sheet/
this is the link from where you can copy the name of the emoji and paste it wherever you want in your blog

Collapse
 
linrstudio profile image
Linr

sorry. my question is how to reverse a strings include emoji ? thank you

Thread Thread
 
swarnaliroy94 profile image
Swarnali Roy

Oh okay.. I'll try to upload that solution ASAP. ☺️

Collapse
 
krishrahul98 profile image
Rahul Krishna

Great content. But most of the above methods will fail if you are using UTF-16 character.

Collapse
 
rafipiccolo profile image
Raphael Piccolo • Edited

Damn useless :) I'm not gonna need any of these functions in my whole life, but you have some interesting solutions here indeed. Nice post

Collapse
 
swarnaliroy94 profile image
Swarnali Roy

Thank you!

Collapse
 
dougaws profile image
Doug

Can you instrument the algorithms? Which is fastest, slowest?