DEV Community

skptricks
skptricks

Posted on

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"

Top comments (0)