If ids is an array, then just use join:

ids = [1, 2, 3, 4];
val = ids.join(', ');
// val is now "1, 2, 3, 4"
Answer from nd. on Stack Overflow
🌐
SitePoint
sitepoint.com › javascript
How to insert a comma in a string - JavaScript - SitePoint Forums | Web Development & Design Community
May 17, 2021 - Hello, I get the current date through this function String(new Date).substring(4, 15) Is there a way to separate the month and day with a comma? Thanks
Discussions

javascript - How cna I a add comma between words in a td if there is more than one word - Stack Overflow
Long backstory on this, and this seems to be the simplest solution. I have a table that will populate with user id's from a database. I need to figure out a way to add a comma between id's if there... More on stackoverflow.com
🌐 stackoverflow.com
javascript - JS: Add comma to a string - Stack Overflow
I'm trying to add comma to a string and show that into label through following function : HTML : More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to add comma in a number in string - Stack Overflow
So I know how to add a comma on numbers (toLocaleString.()) and this function requires integer or decimal value as a parameter. I need this result with 2 digit decimal value. It does run and return... More on stackoverflow.com
🌐 stackoverflow.com
How can I create a comma-separated string in JavaScript using a for loop without adding a trailing comma? - Stack Overflow
Add commas between each repetition. Avoid adding a trailing comma after the last word. This exercise is helping me learn how to use loops effectively for string building, so I’d appreciate an explanation focused on using for loops rather than alternative methods. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Coderwall
coderwall.com › p › nys6wg › easily-add-commas-to-strings-in-javascript
Easily add commas to strings in JavaScript (Example)
February 25, 2016 - I came across this regex sometime ago which adds commas to numbers in JavaScript (e.g. 1040245 -> 1,040,245). When I'm working on an application that involves displaying lots of data, I'll usually include this in my code: String.prototype.commafy = function () { return this.replace(/(^|[^\...
🌐
Medium
medium.com › coding-at-dawn › how-to-convert-an-array-to-a-string-with-commas-in-javascript-79e212506c2
How to Convert an Array to a String with Commas in JavaScript | by Dr. Derek Austin 🥳 | Coding at Dawn | Medium
January 5, 2023 - In fact, the separator string can be anything at all, of any number of characters. That means it’s easy to turn your JavaScript array into a comma-separated list format, whether you’d like to add just a comma or a comma and a space. When you want just the comma character (,) to appear as the separator between ...
Top answer
1 of 3
7

You can specify the exact number of decimal digits in your options, which is the second parameter in toLocaleString()

const number = 24242324.5754;

number.toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
})

// result is: 24,242,324.58

See also MDN doc here

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is

maximumFractionDigits

The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3

2 of 3
1

The toFixed() method returns a string but the toLocaleString() method expects a number so just convert your string to a number after using the toFixed() method with the parseFloat() function and then use the toLocaleString() method on it.

However, do note that you will have to manually append the leading 0 since the parseFloat() method removes any leading zeroes to the right of the decimal point.

Check this particular answer on another Stack Overflow thread that explains the reason why the parseFloat() method removes the leading zeroes after the decimal point.


var num = 66666.7
var parsedNum = (""+num).split('.')[1].length > 1 ?
    parseFloat(num.toFixed(2)).toLocaleString() : 
    parseFloat(num.toFixed(2)).toLocaleString() + '0';

console.log("original", num)
console.log("with comma", num.toLocaleString())
console.log("with 2 digit fixed", num.toFixed(2))
console.log("now working--", parsedNum)

Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-commas-between-a-list-of-items-dynamically-in-javascript
How to Add Commas Between a List of Items Dynamically in JavaScript?
February 6, 2023 - We can use the CSS "::before" pseudo-element to dynamically add a comma before each list item, except for the first one. By targeting the list item and using the "content" property, we can insert a comma before the list item's content. Additionally,
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-add-space-after-each-comma-string
Add Space after each Comma in a String using JavaScript | bobbyhadz
March 4, 2024 - If the separator argument is set to an empty string, the array elements are joined without any characters in between them. You can learn more about the related topics by checking out the following tutorials: Add a Space between the Characters of a String in JS
🌐
If Not Nil
ifnotnil.com › t › add-commas-to-existing-string › 3643
Add commas to existing String - If Not Nil
October 25, 2023 - I need to take a string “S” and insert a comma every 3 characters so convert this ABCDEFGHIJ to this A,BCD,EFG,HIJ note the groups of 3 count from the RIGHT
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript add commas to number
How to Format Number With Commas in JavaScript | Delft Stack
February 2, 2024 - This article will explore different ... with commas using JavaScript. Additionally, we will discuss scenarios such as formatting decimal numbers and handling negative values to ensure accurate and aesthetically pleasing number display. ... One positive lookahead assertion searches for a point in the string containing ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Insert commas between adjacent lower and upper case chars - JavaScript - The freeCodeCamp Forum
September 7, 2021 - Hi, My coding skillz are a bit rusty these days and JavaScript was not one of my languages. I am looking to insert a comma in a string whenever a lower case letter is followed by an upper case letter e.g. Convert ‘AaaBbbCcc’ into ‘Aaa,Bbb,Ccc’ I guess I need to loop through the string ...
🌐
Reddit
reddit.com › r/javascripttips › how do i convert an array to a string with commas in javascript
r/JavaScriptTips on Reddit: How do I convert an array to a string with commas in JavaScript
April 13, 2023 -

In JavaScript, you can convert an array to a string with commas using the join()
method.

The join() method returns a string that concatenates all the elements of an array, separated by the specified separator, which in this case is a comma.

Here is an example:

const array = ['apple', 'banana', 'orange'];
const string = array.join(', ');
console.log(string); 

// output: "apple, banana, orange"

In this example, we first define an array of three fruits. Then we use the join()
method with a comma and a space as the separator to create a string that lists all the fruits with a comma and a space between each one.

You can replace the comma and space separator with any other separator you like, such as a hyphen, a semicolon, or a newline character.

It's important to note that the join() method only works on arrays, and it will throw an error if you try to use it on any other type of object.

Click here to learn more ways to Convert Array to String with Commas in JS