4umi.com/web/javascript/reverse

Reverse-a-txet

Text Javascript

Turn around. Featured is an extension to the native String object, a prototype that puts any given characters in the opposite order.

The script

Traditionally, the reverse method is native to the Array object. The script takes advantage of this by splitting the string into an array with the empty string '' as the argument, which results in an array of single characters. This array is reversed in the usual way (for arrays) and joined back to a string, also with an empty string so as to leave no trace...

No variables need to be created and all called methods belong to the core of the language. Very short and very fast.

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

Let 's try that out:

var s = 'Always look on the bright side of life.';  //  define and declare a string

window.alert( s.reverse() );  //  .efil fo edis thgirb eht no kool syawlA/**/

window.alert( s.reverse().reverse()===s );  //  true/**/

while( s = prompt( 'Your text to reverse...', s ) ) { s = s.reverse(); }/**/
Your text

Always read always.

Reprise

OK, not reversed, but here is Brian.