DEV Community

skptricks
skptricks

Posted on

1 2

How To Reverse A String In JavaScript

Post Link : How To Reverse A String In JavaScript

his tutorial explains how to reverse a string in JavaScript. We are using reverse method, reduce method to reverse a string in javascript.

How to reverse a string in JavaScript

Method - 1 :
We used split method to split the string into an array of individual strings then chain it to reverse method.

const str = "ABCDEFGH"

let getReverseString = str.split('').reverse().join('')

console.log(getReverseString)

Output :

"HGFEDCBA"

Method - 2 :
Reverse a string in traditional way using while loop.

function reverseString(str){

const arr = [...str]
let reverse= "";

while(arr.length){
reverse = reverse + arr.pop()
}

return reverse
}

const stringvalue = "ABCDEFGH"

console.log(reverseString(stringvalue))

Output :

"HGFEDCBA"

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please drop a ❤️ or a friendly comment on this post if it resonated with you!

Okay