Turn a list into a JavaScript array literal

Paste a list from a spreadsheet, a text file or an email and get a ready-to-paste JavaScript array, with strings quoted, numbers left as numbers and duplicates removed.

Live demo · real result from this tool

JavaScript array

const fruits = ["apple", "banana", "cherry", 42];
Items4

Processed on your device — nothing is uploaded.

How to use the list to JS array converter

  1. Paste your items into List.
  2. Leave Split on at Detect, or choose New lines, Commas, Semicolons, Tabs or Spaces.
  3. Choose Quote items: Strings only (numbers unquoted), Single quotes, Double quotes, Backticks or No quotes.
  4. Set the clean-up options: Trim spaces, Skip empty items, Remove duplicates, Sort A–Z and One item per line.
  5. Type a Variable name (optional) to get a const declaration, or clear it for a bare array. Copy the JavaScript array.

What it does and when to use it

Hand-quoting dozens of country codes or a column of product SKUs is slow and error-prone: one missing comma or quote breaks the file. Use this tool when you need test data, a lookup list, an allow-list of IDs or menu options in code, and the source is a spreadsheet, a document or a list someone sent you.

For text-only clean-up without code, use remove duplicates or the text sorter. If the data has several columns, CSV to JSON builds an array of objects instead. To turn a finished array into JSON with proper formatting, run it through JS object to JSON.

How it works

The text is split on the chosen separator. Then, in this order, items are trimmed, empty ones are dropped, duplicates are removed keeping the first one, and the list is sorted. Sorting uses natural order, so 2 comes before 10, and capital letters do not jump ahead of small ones. Each item is then written as a literal: in Strings only mode a value that looks like a number, true, false or null is left bare and everything else is double-quoted. Quotes and backslashes inside an item are escaped for the quote character you picked.

Worked examples

The default list. Five lines with a repeat:

apple
banana
cherry
banana
42

become const fruits = ["apple", "banana", "cherry", 42];. The second banana is removed, so Items shows 4, and 42 stays a number.

Semicolons, single quotes, one per line. red; green; blue; 007 with Split on Semicolons, Single quotes, no variable name and One item per line gives:

[
  'red',
  'green',
  'blue',
  '007',
]

Each item gets a trailing comma, which JavaScript allows.

Sorting mixed values. Zed 10 2 apple 1 split on Spaces with Sort A–Z gives [1, 2, 10, "apple", "Zed"]: numbers in numeric order first, then words ignoring case.

Limits and tips

  • Trailing commas in one-per-line output are fine in JavaScript but not in JSON.
  • No nesting. Every item becomes one value; use a CSV tool for rows with several fields.
  • Separators inside items are not respected, so "Smith, John" splits at the comma. Use New lines for such data.

Frequently asked questions

How does Detect choose the separator?
If the list has more than one line, each line is an item. Otherwise it splits on commas if there are any, then on tabs, and finally on runs of spaces.
Why is 007 quoted but 42 is not?
With "Strings only (numbers unquoted)", plain numbers such as 42, -3.5 or 1e3 are written as numbers, and true, false and null stay unquoted too. A value with a leading zero, such as 007 or a ZIP code, is kept as a string so the zeros are not lost.
Can I paste a column from Excel or Google Sheets?
Yes. A copied column arrives as one value per line, which Detect handles. A copied row arrives tab-separated, which Detect also handles.
Are quotes and backslashes inside items escaped?
Yes. An item such as O'Neil becomes 'O\'Neil' with single quotes and "O'Neil" with double quotes, so the array is always valid JavaScript.
Can I get JSON instead?
Yes, almost. Choose Double quotes or Strings only, leave "One item per line" unticked and clear the variable name. That bare array is valid JSON. One item per line adds a trailing comma, which JSON does not allow.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close