javascript adding commas to numbers

javascript adding commas to numbers easy to do.You can put commas to each 3 decimal places.That means separating the thousands or javascript number format or rounding it.Following functions are returns with commas with called number.
Only you have to do call this function.

Method-1
function commaSeparated(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
}
// Usage
var myValue = 1234567;
alert(commaSeparated(myValue));
Method-2

This is a another function to adding commas to numbers.

function commaSeparated(str) {
	  return (str + "").replace(/\b(\d+)((\.\d+)*)\b/g, function(a, b, c) {
	    return (b.charAt(0) > 0 && !(c || ".").lastIndexOf(".") ? b.replace(/(\d)(?=(\d{3})+$)/g, "$1,") : b) + c;
	  });
}
// Usage
var myValue = 123456789;
alert(commaSeparated(myValue));