Reverse string in JavaScript

JavaScript
voted: 1
vote
reverse string

In JavaScript String (unlike array) hasn't got native reverse function (at least by now). To reverse string we can use both String.prototype or "third party" function. To see full code and demo check out http://jsbin.com/gawiga/2/edit?html,js,output

/************** reverseString function **************/

function reverseString(str) {
  return str.split("").reverse().join("");
}

/****************** Edit prototype ******************/

// Make sure we won't owerwrite native code in the future
if (!('reverse' in String.prototype)) {
  String.prototype.reverse= function() {
    return this.split("").reverse().join("");
  };
}
working url: http://jsbin.com/gawiga/2/edit?html,js,output
version: not entered
Leave a Comment:
Characters left:
No comments yet.