JavaScript – how to trim leading or trailing spaces of a string

There are tons of publications on the subject in Internetm but let me put my “50-cents” on this.
The problem is that in javascript there is no built-in function to trim leading and trailing spaces on a string.
This can be easily implemented by adding required method to the javascript’s String object:

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