Skip to main content

Command Palette

Search for a command to run...

Reverse String DSA Solve In Javascript

Updated
•1 min read•View as Markdown
Reverse String DSA Solve In Javascript
S

Ex Full Stack Developer at @WiseBoxs | Vue React Node | MERN

In this blog I am going to share a important DSA javascript interview question reverse string in javascript.

Algo Explaination:

We will run the loop till the half of the array. We will have two pointers left and right. left is the 0th index and right is the length -1 index. Next in every iteration we will replace the left element with right and right element with left.

var reverseString = function(s) {
    let left = 0;
    let right = s.length-1;
    while(left<=right){
        let temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        left++;
        right--;
    }
};