Trim: remove whitespace from the start and end(both sides) of the string.
LTrim: remove start whitespace of the string.
RTrim: remove end whitespace of the string.
Rather than using a clumsy loop, use a simple, elegant regular expression.


String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}


Example:-

// example of using trim, ltrim, and rtrim
var myString = " hello my name is imdadhusen ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/bSXv-XoYus8/14231