PHP implode() Equivalent in JavaScript
JavaScript's Array.prototype.join() mirrors PHP's implode() closely, including automatic stringification of non-string elements, but its default separator is a comma rather than an empty string.
Close equivalent Covers the common case, but has documented behavioral differences.
- PHP source
implode()- Closest JavaScript API
Array.prototype.join()
Main limitation: JavaScript's `.join()` called with no arguments defaults to a comma (`,`) separator; PHP's `implode($array)` with no glue defaults to an empty string — a silent, easy-to-miss default mismatch.
How to use each function
Before comparing the two, here's how implode() and Array.prototype.join() work
on their own.
PHP: implode()
implode(string $separator, array $array): string
Joins the elements of an array into a single string, placing $separator between each element. Non-string elements (integers, floats, booleans) are automatically converted to strings. The $separator may be omitted, defaulting to an empty string.
Returns: The concatenated string.
implode(", ", ["red", "green", "blue"]);
// "red, green, blue" JavaScript: Array.prototype.join()
arr.join(separator = ',')
Joins array elements into a string, automatically stringifying numbers, booleans, and null (as an empty string) along the way — closer to PHP's implode() than Python's join(). The default separator is a comma if you call .join() with no argument, unlike PHP's empty-string default.
Returns: The joined string.
["red", "green", "blue"].join(", ");
// "red, green, blue" Quick mapping
| PHP | implode() |
|---|---|
| Closest JavaScript API | Array.prototype.join() |
How it differs
- JavaScript's
.join()called with no arguments defaults to a comma (,) separator; PHP'simplode($array)with no glue defaults to an empty string — a silent, easy-to-miss default mismatch. - Both languages automatically stringify non-string elements (numbers, booleans) when joining, unlike Python — this is one of the closer pairings in this list.
nullandundefinedelements are converted to empty strings by JavaScript's.join(); PHP convertsnullto an empty string too, so this edge case matches.
Examples
Basic join
implode(", ", ["a", "b", "c"]);
// "a, b, c" ["a", "b", "c"].join(", ");
// "a, b, c" Default separator mismatch
implode(["a", "b", "c"]);
// "abc" (empty-string default) ["a", "b", "c"].join();
// "a,b,c" (comma default) Edge cases
Edge case
Always pass an explicit separator to JavaScript's .join() when porting PHP code — relying on the default comma when the PHP source used implode($array) (empty-string default) is a common, silent bug.
References
- PHP Manual — implode — checked 2026-07-29
- MDN Web Docs — Array.prototype.join() — checked 2026-07-29
Last verified 2026-07-29.