Two interesting functions in JavaScript I’ve created due to “fix” a few problems I’ve fought against.
Both functins exist in PHP, and I needed to use’em in my client side.
The first one, is the strrev function. Javascript has in the Array prototype, the function reverse, but not in strings. Then, here goes the “one line” function to revert strings in javascript.
function strrev(str)
return str.split(”).reverse().join(”);

We must use it like this:
var rev= strrev(”Felipe Nascimento”);

we also could have put it into the prototype of our strings:

String.prototype.invert= function(){
return this.split(”).reverse().join(”);

In this case, the example would be:
“Felipe Nascimento”.invert();

Everything I did here, was just transform the string into an Array, revert it, and then transform it into a String again.

The second is a more complex function, but a quite useful one. It’s the number_format function, also inherited from PHP.
It’s signature is: numberFormat(num, dec, decPoint, thousandSep)
It receives a number or string, and returns it transformed by those patterns sent to it.
We can use it to change the decimal places(values after , or .), as it depends on the country parttener, and other detalis.
It’s usage is just like this:

alert(numberFormat(’1050.1′, 2, ‘.’, ‘ ‘));
alert(numberFormat(’10.50,1′, 2, ‘,’, ‘.’));
alert(numberFormat(’10050.123456′, 5, ‘,’, ‘ ‘));
alert(numberFormat(’10.50,1234′, 3, ‘~’, ‘.’));

The first example is the more used, in which we are forcing the value to have only 2 decimal places.
We can force it to have as many as we want, which will fill the value with zeros.
We can also choose the decimal point, and also the thousand separator.
Here, follows the code:

function numberFormat(num, dec, decPoint, thousandSep)
if(typeof num == ’string’)
if(num.indexOf(’,')>-1 && num.indexOf(’.')>-1)
{ // fixing strings
if(num.indexOf(’,') > num.indexOf(’.'))
{ // eg.: 10.000,000 => 10000.000
num= num.replace(’.', ”).replace(’,', ‘.’);
}else{ // eg.: 10,000.000 => 10000.000
num= num.replace(’,', ”);
{ // eg.: 10000,000 => 10000.000
num= num.replace(’,', ‘.’);
dec= dec|| 2; // 2 decimal places, as default
num= Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
num= (”+num).replace(’.', (decPoint||’.'));
while(num.split(decPoint)[1].length < dec)
num+= ‘0′; // fixing decimal places, after the decimal point
// apply thousand separatores
num= num.split(decPoint);
num[0]= strrev(strrev(num[0]).replace(/([0-9]{3})/g, ‘$1′+(thousandSep||’ ‘)));
return num.join(decPoint);

I hope it becomes useful to you.