1 var x=12345678;
2 x=x.toString();
3 var lastThree = x.substring(x.length-3);
4 var otherNumbers = x.substring(0,x.length-3);
5 if(otherNumbers != '')
6 lastThree = ',' + lastThree;
7 var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
8 alert(res);