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
🌐
Stack Overflow
stackoverflow.com › questions › 64490780 › how-to-add-commas-in-between-words-in-string-in-javascript
regex - How to add commas in between words in string in Javascript - Stack Overflow
Oh, forgot the comma - just put in the , when you're returning the mapped string 2020-10-22T22:16:43.077Z+00:00 ... okay, this didn't have the commas so I added them, final code that works: function meeting(s) { const output = s .toUpperCase() .split(';') .sort((a, b) => { const [aFirst, aLast] = a.split(':'); const [bFirst, bLast] = b.split(':'); return aLast.localeCompare(bLast) || aFirst.localeCompare(bFirst); }) .map((name) => { const [first, last] = name.split(':'); return (${last}, ${first}); }) .join(''); return output; } 2020-10-22T22:19:58.69Z+00:00
🌐
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

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
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
Add Commas to JavaScript output - Stack Overflow
I'm using the following script to count upward at an interval and it works perfectly. However, I'd like it to format the number with commas (56,181,995 instead of 56181995). var START_DATE = new D... More on stackoverflow.com
🌐 stackoverflow.com
November 8, 2011
🌐
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(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) { return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,"); }); }; If you're working with non-Strings, you can add this function as well: Number.prototype.commafy = function () { return String(this).commafy(); }; Calling it thereafter is child's play: var foo = "1234567" var foobar = foo.commafy() Or, more realistically: var clicks = item[totalClicks].commafy(); #javascript ·
🌐
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 items in the list, use .join(",") to convert the array to a string.
🌐
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 - Alternatively, you can also use javascript or jquery to dynamically add commas between the list items.
Find elsewhere
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)

🌐
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 - Then the replacement expression inserts a comma at that position. It also takes care of the decimal places by splitting the string before applying the regex expression on the part before the decimal.
🌐
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
🌐
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 - The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array. This is a more manual approach of replacing each comma with a comma and a space.
🌐
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 checking for lower followed by upper What is the basic string loop syntax and is there an isUpper isLower function you can call on a char variable extracted from a string something like:- if(myString[3]....
🌐
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

🌐
sebhastian
sebhastian.com › javascript-format-number-commas
JavaScript format number with commas (example included) | sebhastian
July 8, 2022 - You can use the regex pattern in combination with String.replace() to replace the markers with commas. ... function numberWithCommas(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } let n = numberWithCommas(234234.555); ...