Coding Tutorial

Convert a List to JavaScript, Python, and PHP Arrays

Learn how to convert a plain text list into JavaScript arrays, Python lists, and PHP arrays with proper quoting, escaping, and copy-ready examples.

When you have a plain list of items—perhaps copied from a spreadsheet, exported from a database, or typed out in a text file—you often need to convert it into an array literal for use in your code. Manually adding brackets, commas, and quotes is tedious and error-prone, especially if the list is long or contains special characters.

Different programming languages have similar but distinct syntaxes for arrays. JavaScript uses square brackets for arrays, Python uses square brackets for lists, and PHP supports both array() and square brackets. Strings require quotes in all three languages, while numbers are written without quotes. Understanding these nuances ensures your converted array is syntactically correct.

This guide compares the conversion process for JavaScript, Python, and PHP. It covers quoting rules, escaping, and handling mixed data types. We also introduce free browser-based tools from CompareTwoLists that automate these conversions with a single paste, keeping your data private.

The Starting Point: A Plain Text List

Before converting, identify your raw input. The simplest case is a list of strings, each on a separate line. This input can come from various sources: a column in a CSV file, a list copied from a spreadsheet, or from a SELECT output in SQL. For our examples, we'll use a list of fruit names.

  • One item per line
  • No extra formatting or punctuation
  • Empty lines are typically ignored
Sample Input List
apple
banana
cherry
date

JavaScript Array Syntax and Conversion

In JavaScript, arrays are enclosed in square brackets. Elements are separated by commas. Strings must be wrapped in either single or double quotes. The conversion of our fruit list produces a clean array literal.

You can assign the array to a variable using const, let, or var. Choose const for values that won't be reassigned. Avoid trailing commas for maximum compatibility, though modern JavaScript allows them.

  • Wrap each item in quotes
  • Separate by commas
  • Enclose everything in square brackets
JavaScript Array
const fruits = ['apple', 'banana', 'cherry', 'date'];

Python List Syntax and Conversion

Python lists also use square brackets and commas. Python's convention is to use single quotes for strings, although double quotes are acceptable. The resulting list is a first-class collection type with numerous built-in methods.

As in JavaScript, you assign the list to a variable. Python's list comprehension and built-in functions make working with lists very flexible. The conversion is straightforward, and trailing commas are allowed in Python.

  • Use square brackets and commas
  • Single quotes are customary for strings
  • Trailing comma is valid
Python List
fruits = ['apple', 'banana', 'cherry', 'date']

PHP Array Syntax and Conversion

PHP provides two ways to declare arrays: the older array() function and the short syntax [] introduced in PHP 5.4. The short syntax is now widely used and recommended for new code. PHP requires a $ prefix on variable names.

Both single and double quotes are supported in PHP. Double quotes allow variable interpolation and escape sequences, but for simple string arrays, single quotes are slightly more efficient and avoid unintended parsing.

  • Use $variableName = [...] or $variableName = array(...)
  • Do not use trailing commas (not allowed in PHP)
  • Prefer single quotes for simple strings
PHP Array (Short Syntax)
$fruits = ['apple', 'banana', 'cherry', 'date'];

Handling Strings Versus Numbers

A common concern is whether to treat an item as a string or a number. In the target array, numbers should not be quoted—they must be numeric literals. If the source list contains numeric-looking values like ZIP codes, product IDs, or phone numbers, you must decide based on context. If you need to preserve leading zeros, treat them as strings.

When the data type is ambiguous, it is safer to quote everything as strings, then adjust manually for numeric values. For example, a list with a mix of names and ages requires careful separation.

  • Numbers: no quotes
  • Strings: always quotes
  • Ambiguous values: default to string
Mixed Types in JavaScript
const mixed = ['Alice', 30, 'Bob', 28];
Mixed Types in Python
mixed = ['Alice', 30, 'Bob', 28]
Mixed Types in PHP
$mixed = ['Alice', 30, 'Bob', 28];

Escaping Special Characters and Quotes

When an item contains a quote character that matches the style you are using, it must be escaped. For example, the name O'Brien contains a single quote. If you wrap strings in single quotes, you must escape it as O\'Brien. Alternatively, use double quotes to avoid escaping: "O'Brien" works without modification.

Similarly, if a string contains double quotes, you can wrap it in single quotes. Backslashes and newline characters also require escaping. Choose the quote style that results in the fewest escapes for readability.

  1. Identify all quote characters in the item
  2. If the item contains single quotes, either escape them (\') or use double quotes
  3. If the item contains double quotes, either escape them (\") or use single quotes
  4. Escape any included backslashes by doubling them (\\)
  5. Remove any newlines inside items or convert them to \n
  • Escaping differs between single and double quote contexts
  • Alternate quote styles to minimize escapes
  • Use tools to automate proper escaping
Escaping in JavaScript
const names = ["O'Brien", 'Say "hello"', 'back\\slash'];
Escaping in Python
names = ["O'Brien", 'Say "hello"', 'back\\slash']
Escaping in PHP
$names = ["O'Brien", 'Say "hello"', 'back\\slash'];

Using CompareTwoLists for Instant Conversion

CompareTwoLists provides separate converters for JavaScript arrays, Python lists, and PHP arrays. Each one treats every input line as a string, applies the selected single- or double-quote escaping rules, and can output a compact or multi-line result.

The JavaScript tool can emit a const declaration, let declaration, or array literal. The Python and PHP tools accept an optional variable name. They do not infer numeric or boolean types, and they do not add trailing commas, so review and edit types explicitly when the destination code requires them.

  1. Open the converter for the target language.
  2. Paste one intended string value per line.
  3. Choose single or double quotes, layout, and the variable or declaration option.
  4. Copy the result and run it through the target language's formatter or test suite.
  • Local in-page processing
  • Language-specific string escaping
  • Every input line is emitted as a string
  • No automatic type inference or trailing comma option

Conclusion

Array syntax is similar across JavaScript, Python, and PHP, but quoting, escaping, variable declarations, and data types still need deliberate choices. Treating pasted lines as strings is a safe default for IDs and human-entered values.

The browser converters create copy-ready string collections locally. Review inferred meaning yourself, then format or test the result in the destination language before committing it to an application.

FAQ

Frequently asked questions

How do I handle empty lines in my list?+

Most conversion tools ignore empty lines. For manual conversion, skip them or decide to include them as empty strings. If you intentionally want an empty string in your array, insert two quotes with nothing between ('').

What if my list contains commas or quotes?+

Commas are safe inside quoted strings. Quotes need escaping: use a backslash before the quote within the string, or wrap the string with the opposite quote type to avoid escaping.

Can I convert a list directly to JSON?+

Use a JSON serializer or the List to JSON tool when JSON is the target. A JavaScript array literal is valid JSON only when it uses JSON syntax: no variable declaration, double-quoted strings, no comments, no undefined value, and no trailing comma. Python repr and PHP array syntax are not JSON.

Which languages allow trailing commas?+

Modern JavaScript, Python, and PHP all allow trailing commas in array or list literals in commonly used versions, although compatibility depends on the exact runtime and context. The CompareTwoLists converters emit output without a trailing comma, which is the most portable default.

Why are numbers not quoted?+

An unquoted numeric token becomes a number, while a quoted token remains a string. The browser converters intentionally quote every input line, so change a value manually only after confirming its required type. Keep phone numbers, ZIP codes, account IDs, and leading-zero values as strings.

Related tools

Popular list tools

Browse all tools →

Guides

Related guides

Back to all guides →