Blog

In JavaScript Reverse a String without using reverse()
Posted on November 12, 2017 in Algorithms, JavaScript by Matt Jennings

var str = 'blah';

function reverseStr(str1) {
    var arr1 = str1.split('');
    var arr2 = [];

    for(var i = arr1.length - 1; i > -1; i--) {
        arr2.push(arr1[i]);
    }

    var str2 = arr2.join('');

    return str2;
}

// Output below is 'halb'
console.log(reverseStr(str));

 

Leave a Reply