Welcome, guest | Sign In | My Account | Store | Cart

Two ways reverse string.

JavaScript, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//with cycle (not recommended)
function StrReverse(str) {
  var res = '';
  for (var i = str.length - 1; i >= 0; --i)
    res += str.charAt(i);

  return(res);
}

alert(StrReverse(".gnirts a si sihT"));

//creating prototype
String.prototype.reverse = function() {
  return this.split('').reverse().join('');
}

alert(new String(".gnirts a si sihT").reverse());
Created by greg zakharov on Mon, 18 Feb 2013 (MIT)
JavaScript recipes (69)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks