Turning a pasted list of words into a JavaScript array is a daily chore for developers: test data, dropdown options, tags, or sample names. Doing it by hand means adding quotes and commas one item at a time. This guide shows you the clean method, including the quoting traps.
What the result looks like
A JavaScript array of strings looks like this: [“apples”, “bananas”, “oranges”]. Each item sits in quotes, items are separated by commas, and the whole list is wrapped in square brackets. A number array drops the quotes: [10, 25, 40]. Mixing the two by accident is a common source of bugs.
Step-by-step: format a list into an array
- Put one item per line. This is the easiest input shape to work from.
- Remove blank lines and trim spaces from the ends of each item.
- Decide: strings or numbers? If every item is a number and you want to do maths with them, leave the quotes off. Otherwise quote everything.
- Wrap each item in double quotes, and place a comma after each item except the last.
- Wrap the whole result in square brackets.
- Paste it into your code and check the console for syntax errors before moving on.
The quoting and escaping traps
Items that contain a double quote will break your array unless the quote is escaped with a backslash, like “say “hello””. Items with apostrophes are safe inside double quotes, but break inside single quotes. Backslashes themselves must be doubled. If your list contains all three, consider using a formatter that handles escaping for you rather than fixing it by hand.
Numbers versus strings deserve a second look too. An array of [“10”, “25”] holds strings, and adding them gives “1025”, not 35. If you plan to calculate with the values, they must be unquoted numbers.
Try it the faster way
Paste your list into the tool below to format it as a JavaScript array instantly.
Prefer the full page? Open the JavaScript array formatter directly.
Quick tips
- Keep the one-item-per-line version as your master copy.
- Check the first and last items; errors hide at the edges.
- Run the result through a linter or the browser console before committing it.
Array formatting is mechanical work, which makes it perfect for a tool. Set strings versus numbers once, let the tool handle the quotes, and check the result.
Frequently asked questions
Should I use single or double quotes in a JavaScript array? Either works, as long as you are consistent. Double quotes are safer when items contain apostrophes, and single quotes are safer when items contain double quotes. Pick the style that needs less escaping for your data.
What is a trailing comma, and is it allowed? A trailing comma is a comma after the last item, like [“a”, “b”,]. Modern JavaScript allows it in arrays, and many formatters add it deliberately, since it makes adding items easier. Older tools and JSON do not allow it, so strip it when producing JSON.
How do I format an array of objects instead of strings? The same principles apply: one object per line, consistent quoting, commas between items. Formatters built for simple lists may not handle objects, so for object arrays, use a JSON formatter or build the array in code.
Worked example
Turn this list into an array: red, green, blue. Quoted as strings it becomes [“red”, “green”, “blue”]. Now try numbers: 10, 20, 30. As strings they become [“10”, “20”, “30”], but as numbers they become [10, 20, 30], and the difference matters: adding the string versions gives “102030”, while adding the number versions gives 60. The tricky item is a quote inside a value, such as say “hi”: it must be escaped as “say \”hi\”” or the array breaks. A formatter handles that escaping for you.
