Home > JavaScript > String Reverse

String Reverse

JavaScript does not provide a method for reversing a string either as a built-in function or a method associated with the String object. Here is a small routine through which you’ll be able to do it.

The working

This method accepts a string and split the string to an array, reverse the array and join the reversed array into a string. Return that reversed string from the function.

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

The same thing that we’ve achieved through the above function can be achieved the function furnished below:

function reverseString(str){
	return Array.prototype.reverse.apply(str.split('')).join('');
}

* Note that all the quotes used in the above mentioned code are single quotes

Advertisements
Categories: JavaScript
  1. Thomas
    January 29, 2009 at 3:47 am

    thats a pretty handy function you have there. shud be of help to most of the developers out there

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: