JavaScript: How to Remove All Commas From a Number

If this post helps you, click an ad

The following can be used in JavaScript to remove all commas from a number:

var StartNumber = 444,555,666;
var ReplacedNumber = StartNumber.replace(/\,/g,
);
document.write(ReplacedNumber);

The preceding example uses the JavaScript replace function and utilizes a regular expression to replace all the commas in a number. In the regular expression, the backslash (\) before the comma is used as an escape character, meaning that the comma should be treated as a literal comma. The g means that the replace should be applied globally. Without this, only the first comma would be removed.

Related articles:
JavaScript replace Function
JavaScript Regular Expression Backslash

11 Responses to “JavaScript: How to Remove All Commas From a Number”

  1. JDub Says:

    Ahh, finally a comma replace script that works for me! And it’s so simple! I kept trying other replace scripts, and they would all give me errors, but this one works great. Thanks!

  2. errol Says:

    thanks !! it worked well (Y)

  3. indnajns Says:

    Ditto JDub. This one WORKS! Thank you so much, Joey.

  4. bubba Says:

    shouldn’t “FormNumber” in the 2nd line actually be StartNumber??

  5. Kris Says:

    Worked great, thanks!

  6. Joey Says:

    Bubba, good catch. This has been corrected in the post. :)

  7. Pete B Says:

    Worked great! Thanks!

  8. BmX Says:

    Thanks dude. it worked fine.

  9. Ratheesh Kannan Says:

    Thats worked awesome, thanks for the info :-)

  10. tokes Says:

    Just what I needed….and works perfectly

  11. infoct Says:

    thanks man! it helped me

Leave a Reply